I want to control and run two pieces of lab equipment at the same time (at least starts them simultaneously), so I use multiprocessing module.
from myLabModule import Newport1936R
from myLabModule import DCx_camera
from multiprocessing import Process, Queue
def multiprocess_camera(camera, shots_per_img, queue, delay):
time.sleep(delay)
img, camera_time = camera.capture(shots_per_img, np.float64, True) #img is a 2x2 ndarray
results = [camera_time[0], camera_time[1], img, 'img']
queue.put(results)
def multiprocess_power(newport, interval, N, queue, delay):
time.sleep(delay)
power_reading, newport_time = newport.get_power(interval, N, True)
results = [newport_time[0], newport_time[1], power_reading, 'power']
queue.put(results)
if __name__ == "__main__":
thorcam = DCx_camera()
newport = Newport1936R()
queue = Queue()
p1 = Process(target=multiprocess_camera, args=(thorcam,10,queue,0))
p2 = Process(target=multiprocess_power, args=(newport,1,10,queue,0))
p1.start()
p2.start()
p1.join()
p2.join()
results = []
results.append(queue.get())
results.append(queue.get())
When I run the code, I get the following error: AttributeError: Can't pickle local object 'CDLL.__init__.<locals>._FuncPtr'. I read this post, it seems that it has to do with the scope of the variables (I am passing two classess defined in another module to the functions, and the functions basically run the classes' methods). What I'm not sure exactly which part is causing the problem. How should I modify the code to make it work?
thorcam.capture()andnewport.get_power()at the same time (using multiprocessing) and also get their return values. I just checked a few relevant posts and it seems that making a separate functions is the way to do it, of course I'm open to other better methods.thorcamandnewportbellow processes are started.thorcamandnewportin the current script? I can do so, the only problem is the module I have written forthorcamandnewportare a few hundred lines long, so I think it looks cleaner if I keep them as separate files. Is it possible to just redefine the classes in a few lines and referring the actual code to the separate files?args=(thorcam,10,queue,0).thorcamhas to be pickled and I'm not sure howDCx_camerais implemented.