python concurrent process -electrical engineering usage

105 Views Asked by At

I am an electrical engineer trying to do multiprocessing in python2.7. I have two oscilloscopes which need to run the same tests on 2 different signals.

Right now, I have a code which does it in sequence and takes long time.

I want to make the measurements simultaneously on both scopes and put the results into logging function, one after the other properly.

I am trying to dabble with the multiprocessing or concurrent.futures which ever helps me.

Here is the point where I need help.

My tests are python functions

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

Below is my main loop

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. How do I make it to parallel process scope1 and scope2 simultaneously?

Q2. How to handle logging?

Will be very helpful if anybody can guide me

EDIT: OK.. I tried both multiprocess and multithread approach, and multiprocess approach is the fastest (obviously). But now logging is still a problem.

What I tried

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

Executes beautifully, but logging is not working.

2

There are 2 best solutions below

3
On

Q1 answer:

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

1
On

Something like:

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