0

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

现在,我有一个代码可以按顺序执行并且需要很长时间。

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

我正在尝试使用对我有帮助的multiprocessingconcurrent.futures

这是我需要帮助的地方。

我的测试是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()

Q1。如何使其同时并行处理范围 1 和范围 2?

Q2。如何处理日志记录?

如果有人可以指导我,将非常有帮助

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

我试过的

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()

执行得很漂亮,但日志记录不起作用。

4

2 回答 2

0

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

这是python线程文档的链接

于 2016-02-21T08:15:12.027 回答
0

就像是:

#!/usr/bin/env python2

import threading
import urllib2

def test1(scope_num):
    response = urllib2.urlopen('http://www.google.com/?q=test1')
    html = response.read()
    return 'result from test1, ' + scope_num

def test2(scope_num):
    response = urllib2.urlopen('http://www.google.com/?q=test2')
    html = response.read()
    return 'result from test2, ' + scope_num

def test_scope(scope_num, results):
    print 'starting tests for ' + scope_num
    results[scope_num] = [test1(scope_num), test2(scope_num)]
    print 'finished tests for ' + scope_num

def control_prog():
    results={}

    for temperature in [0,25,100]:
        for voltage in [600,700,800]:
            print 'testing scope1 and scope 2'
            thread1 = threading.Thread(target=test_scope, args=('scope1', results))
            thread2 = threading.Thread(target=test_scope, args=('scope2', results))
            thread1.start()
            thread2.start()
            # wait for both threads to finish
            thread1.join()
            thread2.join()
            print 'testing scope1 and scope 2 finished'

            print results

control_prog()
于 2016-02-21T09:48:33.187 回答