Starting threads

Tim Peters tim_one at email.msn.com
Tue Jan 28 22:41:23 EST 2003


[Lukas Kubin]
>> import threading
>>
>> class Control(threading.Thread):
>>
>>   def run(self):
>>     doTheAction()
>>
>> if __name__ == '__main__':
>>
>>   for comp in Computers:
>>     thread = Control()
>>     thread.start()
>>     time.sleep(.005)   # <-- This is what I still need to use

[James J. Besemer]
> Without the sleep (and even with it) your main thread can exit
> immediately after starting the "Control" threads.

That's so.

> This can be before the threads finish and even before some or all
> of them actually really get started.

That too.

> There may be some implementations where the main thread waits for the
> other threads to finish but on some implementations, exiting the main
> thread definitely kills off the other threads whether they finish or
> not.

That's true of threads created by the thread module, but not of threads
created by the threading module (which Lukas is using):  Python arranges for
the main thread to do (an implicit) t.join() on every thread t created by
the threading module before allowing the main thread to exit, except for
threads explicitly set to be daemon threads.

I didn't find the OP's original post clear enough to guess what the real
problem was.  Knowing the Python version and OS would help.






More information about the Python-list mailing list