showing why lock is need if you have multiple process (uuts to test) but only One DMM
Focus is on Multiprocessing and Lock (not on DMM communication setup)
code for Python3.7 used in Spyder
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 14:32:12 2023
@author: aleja
"""
from multiprocessing import Process,Queue,Lock
import time
def DMM_Voltage(UUT_Slot,UUT_Ch,lock_dmm,qu):
# only ONE dmm so LOCK (before lock 1sec) (after 4sec) each but each process was call its own dmm_volt
#which in real use is a problem ONE DMM lock force function to block other use dmm_volt
# in this simulation you alway see correct meas BUT in real dmm imagen dmm getting different uuts reading
lock_dmm.acquire()
time.sleep(0.000001) #for me the lock needed time to enforce will try to move into test uut to see diff
qu.put(("setting up DMM mux for UUT:"+str(UUT_Slot)+"\n"))
time.sleep(0.1)
meas_Volts=(UUT_Slot*10)+UUT_Ch #simple voltage
lock_dmm.release()
return meas_Volts
def Test_UUT(Slot,UUT_Ch_List,qu,lock_dmm):
str1=""
start_t = time.time()
UUT_Ch_List=[0,1,2,3,4,5,6,7,8,9]
for UUT_Channel in UUT_Ch_List:
Measure=(DMM_Voltage(Slot,UUT_Channel,lock_dmm,qu))
str1=("UUT: "+ str(Slot)+ " Ch: " + str(UUT_Channel) + " Meas: "+ str(Measure)+"\n")
# this will not not print in spyder console print("inside Test UUT")
qu.put((str1))
end_t = time.time()
delta_t=end_t-start_t
str1=("Slot Finished: " + str(Slot)+ " delta time:" + str(delta_t)+"\n")
qu.put((str1))
return (str1)
if __name__=="__main__":
Num_of_UUTs=4
UUT_Slot_List=[0,1,2,3]
UUT_Channel_List=[0,1,2,3,4,5,6,7,8,9]
#for Slot in UUT_Slot_List:
#print(Test_UUT(Slot,UUT_Channel_List))
qu=Queue()
#need a better way to lanuch same process multiple times
lock_dmm=Lock()
pList = [Process(target=Test_UUT, args=(UUT_Slot_List[i],UUT_Channel_List,qu,lock_dmm,)) for i in range(Num_of_UUTs)]
for p in pList:
p.start()
# not this one start_t = time.process_time()
start_t = time.time()
for p in pList:
p.join()
end_t = time.time()
print("total delta time" + str(end_t-start_t) + "\n")
while not qu.empty():
print(qu.get())
End Code
Notes
- Presentation: example One DMM and Python Multiprocessing w Lock
- Programming Language used: Python 3.7 in Spyder
- Presentation app: Microsoft’s PowerPoint
- Presentation shown to spark ideas of use.
- This presentation is not connected to or endorsed by any company.
- Use at your own risk.
- Tags: Python, Python3.7, Multiprocess, Multiprocessing, Lock, Multiple UUT one DMM, Parallelism, Parallel, Run one function many times in parallel, one function in Multiprocessing needs Lock, Lock need some time to Lock, Multiple UUT Testing slot with same test, Queues