orenma
orenma

Reputation: 1273

join() threads without holding the main thread in python

I have a code that calls to threads over a loop, something like this:

def SubmitData(data):
    # creating the relevant command to execute
    command = CreateCommand(data)
    subprocess.call(command)

def Main():
    while(True):
        # generating some data
        data = GetData()
        MyThread = threading.Thread(target=SubmitData,args=(data,)) 
        MyThread.start()

obviously, I don't use join() on the threads. My question is how to join() those threads without making the main thread wait for them? Do I even need to join() them? what will happend if I won't join() them?

some important points:

I'm using threading for Performance so if someone have a better idea instead, I would like to try it out.

Upvotes: 1

Views: 640

Answers (1)

jfs
jfs

Reputation: 414207

Popen() doesn't block. Unless CreateCommand() blocks, you could call SubmitData() in the main thread:

from subprocess import Popen

processes = []
while True:
    processes = [p for p in processes if p.poll() is None] # leave only running
    processes.append(Popen(CreateCommand(GetData()))) # start a new one

Do I even need to join() them? what will happend if I won't join() them?

No. You don't need to join them. All non-daemonic threads are joined automatically when the main thread exits.

Upvotes: 1

Related Questions