python并行过程电气工程

2024-04-18 23:28:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一名电气工程师,尝试在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()

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


Tags: 函数infordefsetupresultnumtalk
2条回答

比如:

#!/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()

问题1答案:

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

Here is the link to the python thread docs

相关问题 更多 >