Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python并发进程-电气工程使用_Python_Python 2.7_Python Multiprocessing_Concurrent.futures_Oscilloscope - Fatal编程技术网

python并发进程-电气工程使用

python并发进程-电气工程使用,python,python-2.7,python-multiprocessing,concurrent.futures,oscilloscope,Python,Python 2.7,Python Multiprocessing,Concurrent.futures,Oscilloscope,我是一名电气工程师,试图在python2.7中进行多处理。 我有两个示波器,需要在两个不同的信号上运行相同的测试 现在,我有一个按顺序执行的代码,需要很长时间 我想在两个示波器上同时进行测量,并将结果一个接一个地正确地输入日志功能 我正在尝试涉足多处理或并发.期货,这对我很有帮助 这就是我需要帮助的地方 我的测试是python函数 def test1(scope_num): recall_setup() #talk to scope over network a = read_m

我是一名电气工程师,试图在python2.7中进行多处理。 我有两个示波器,需要在两个不同的信号上运行相同的测试

现在,我有一个按顺序执行的代码,需要很长时间

我想在两个示波器上同时进行测量,并将结果一个接一个地正确地输入日志功能

我正在尝试涉足多处理并发.期货,这对我很有帮助

这就是我需要帮助的地方

我的测试是python函数

def test1(scope_num):
    recall_setup() #talk to scope over network
    a = read_measurments()
    return a

def test2(scope_num):
    recall_setup() #talk to scope over network
    a = read_measurments()
    return a
下面是我的主循环

scope1=scipycmd(ip_addr_1)
scope2=scipycmd(ip_addr_2)

def control_prog():
    result=emptyclass()

    for temperature in [0,25,100]:
        for voltage in [600,700,800]:

            init_temp_volt(temperature, voltage)

            for scope in [scope1,scope2]:
                result.test1 = test1(scope)
                result.test2 = test2(scope)

                logfile.write_results(results)

control_prog()
问题1。如何同时并行处理scope1和scope2

问题2。如何处理日志记录

如果有人能指导我,我会很有帮助的

编辑:确定。。 我尝试了多进程和多线程方法,而多进程方法是最快的(显然)。但是现在日志记录仍然是一个问题

我试过的

scope=([scope0],[scope1])
def worker():
    test1()
    test2()

def mp_handler(var1):
    for indata in var1:
        p = multiprocessing.Process(target=worker, args=(indata[0]))
        p.start()
执行得很好,但日志记录不起作用。

Q1回答:

import thread
#for Loop here
thread.start_new_thread ( test1, scope_num ) # start thread one
thread.start_new_thread ( test2, scope_num ) #start thread two

类似于:

#/usr/bin/env蟒蛇2
导入线程
导入urllib2
def测试1(范围编号):
response=urllib2.urlopen('http://www.google.com/?q=test1')
html=response.read()
返回'result from test1',+scope_num
def测试2(范围编号):
response=urllib2.urlopen('http://www.google.com/?q=test2')
html=response.read()
返回“来自test2的结果”+scope_num
def测试范围(范围数量、结果):
打印“开始测试”+scope\u num
结果[scope\u num]=[test1(scope\u num)、test2(scope\u num)]
打印“已完成的测试”+scope\u num
def控制程序():
结果={}
对于[0,25100]中的温度:
对于[600700800]中的电压:
打印“测试范围1和范围2”
thread1=threading.Thread(target=test_scope,args=('scope1',results))
thread2=threading.Thread(目标=测试范围,参数=('scope2',结果))
thread1.start()
thread2.start()
#等待两个线程都完成
thread1.join()
thread2.join()
打印“测试范围1和范围2已完成”
打印结果
控制程序

这么简单的东西不需要复杂的锁系统!只需启动两个线程(单独的进程,可能将结果保存到一个变量),然后记录结果Test1和Test2必须是连续的。Scope1和Scope2必须是并行的(这在for循环中)。据我所知,无法同时计算它们,但您可以分别计算它们,并返回一个元组/对象列表供以后使用谢谢!我试过这个方法。它不能提供很好的并行处理。但是日志记录更容易。现在,多进程工作起来很有魅力,但试图找出日志记录。