multithreading - join() threads without holding the main thread in python -
i have code calls threads on loop, this:
def submitdata(data): # creating relevant command execute command = createcommand(data) subprocess.call(command) def main(): while(true): # generating data data = getdata() mythread = threading.thread(target=submitdata,args=(data,)) mythread.start() obviously, don't use join() on threads. question how join() threads without making main thread wait them? need join() them? happend if won't join() them?
some important points:
- the while loop suppose long time (couple of days)
- the command not long (few seconds)
i'm using threading performance if have better idea instead, try out.
popen() doesn't block. unless createcommand() blocks, call submitdata() in main thread:
from subprocess import popen processes = [] while true: processes = [p p in processes if p.poll() none] # leave running processes.append(popen(createcommand(getdata()))) # start new 1 do need join() them? happend if won't join() them?
no. don't need join them. non-daemonic threads joined automatically when main thread exits.
Comments
Post a Comment