0

私はpython2.7でマルチプロセッシングをしようとしている電気技師です。2 つの異なる信号で同じテストを実行する必要がある 2 つのオシロスコープがあります。

現在、順番に実行し、長い時間がかかるコードがあります。

両方のスコープで同時に測定を行い、結果をログ機能に適切に入れたいと思っています。

私はこれまでに役立つマルチプロセッシングまたはコンカレント.フューチャーに手を出そうとしています。

ここが助けが必要なポイントです。

私のテストは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. scope1 と scope2 を同時に並列処理するにはどうすればよいですか?

Q2. ロギングの処理方法

誰かが私を導くことができれば非常に役に立ちます

編集:OK..マルチプロセスとマルチスレッドの両方のアプローチを試しましたが、マルチプロセスのアプローチが最速です(明らかに)。しかし、今でもロギングは依然として問題です。

私が試したこと

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

何かのようなもの:

#!/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 に答える
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 に答える