Starting threads

Lukas Kubin kubin at opf.slu.cz
Wed Jan 29 16:03:46 CET 2003


It's been half a day since I sent a more describing response to the news.
Until now it didn't appear there. So I will try to be more clear now
again:
(I'm running it on linux and python 2.2.1)

The main call looks like:

if __name__ == '__main__':

  for comp in ['hostname1','hostname2']:
    thread = Control(comp)
    thread.start()
    time.sleep(.005)

The doTheAction() function uses socket to connect to a computer and send a
command there.
When I don't use the time.sleep() then it starts 2 threads of 'hostname2'
so it connects 2 times to hostname2 but not to hostname1.
Thank you

lukas


On Tue, 28 Jan 2003, Tim Peters wrote:

> [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.
>
>
>

-- 
Lukas Kubin

phone: +420696398285
email: kubin at opf.slu.cz

Information centre
The School of Business Administration in Karvina
Silesian University in Opava
Czech Republic
http://www.opf.slu.cz





More information about the Python-list mailing list