Threads in Python version 1.5, thread doesn't start until calling process dies

Ype Kingma ykingma at accessforall.nl
Wed Jul 25 22:00:19 CEST 2001


Ken Tabacchi wrote:
> 
> I am having some trouble with a threadded process in Python version
> 1.5.  It seems that the thread does not start until the calling
> process has died.  The thread starts immediately when the following
> program is run under Python version 2.1.
> 
> **********************************************
> import thread, time
> 
> def Thread1():
>     while (1):
>         print "I`m in the thread"
>         time.sleep(1)
> 
> thread.start_new_thread(Thread1, ())
> print "about to call time.sleep(4)"
> time.sleep(4)
> print "just slept for 4 seconds"
> **********************************************
> 
> This is the Python version 1.5 execution of the above code:
> 
> about to call time.sleep(4)
> just slept for 4 seconds
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> 
> and here is the Python version 2.1 execution of the same code:
> 
> about to call time.sleep(4)
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> just slept for 4 seconds
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> I`m in the thread
> 
> Does any one have any suggestions?

Although the first output is unlikely, this might happen when
the python process gets no more CPU time after starting the 
4 second sleep().

However, you should never rely on the process/thread scheduler(s)
for the correct behaviour of your program.
In case you want threads to wait for each other in any
reliable way, have a look at the threading module for inspiration.

> 
> Thanks in advance for your assistance.
> 
> Ken Tabacchi

My pleasure,
Ype

-- 
email at xs4all.nl



More information about the Python-list mailing list