Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > wierd threading behavior

Reply
Thread Tools

wierd threading behavior

 
 
Qun Cao
Guest
Posts: n/a
 
      10-14-2005
Hello,

I am just starting to play threading in python, here is a really
interesting problem I am very curious about:
"
import thread
def main():
thread.start_new(test.())

def test():
print 'hello'

main()
"
this program doesn't print out 'hello' as it is supposed to do.
while if I change main() into :
"
def main():
while 1:
thread.start_new(test.())
"
It goes on to print 'hello' forever.

while if I use:
"
def main():
for i in range(5):
print i
thread.start_new(test.())
"
It prints out 1,2,3,4,5 in main(), but still doesn't print out anything
from test()!

This is really wierd behavior for me, I am sure it's just something
simple&stupid, please enlighten me!

Thanks,
qun

 
Reply With Quote
 
 
 
 
Sam Pointon
Guest
Posts: n/a
 
      10-14-2005
thread is a low-level threading module, and start_new is deprecated.
Aside from that, thread.start_new(test.()) is syntaxically wrong (a
pair of brackets can't follow a dot). However, your example does work
for me once I fix the syntax, and it prints hello but then hangs. I
can't explain the other results, though - possibly undefined behaviour
or more likely me not having much experience with the low-level thread
interface.

Use threading instead, like so:

import threading

def test():
print 'Test successful!'

def main():
thread = threading.Thread(target = test)
thread.start()

main()

 
Reply With Quote
 
 
 
 
Qun Cao
Guest
Posts: n/a
 
      10-14-2005
Thanks Sam,
That was a stupid typo ( yeah, I actually typed it in , it should be
(test,()).
I am using python 2.4.1 in ubuntu. I do aware that threading.Thread is
prefered,
but I did not realize thread is deprecated. It is still a mysterious
behavior anyhow.

 
Reply With Quote
 
Neil Hodgson
Guest
Posts: n/a
 
      10-15-2005
Qun Cao:

> import thread
> def main():
> thread.start_new(test.())
>
> def test():
> print 'hello'
>
> main()
> "
> this program doesn't print out 'hello' as it is supposed to do.
> while if I change main()


The program has exited before the thread has managed to run. It is
undefined behaviour whether secondary threads survive main thread
termination but it looks like they don't on your system.

Use the threading module and call join to wait for all threads to
complete before exiting the program.

Neil
 
Reply With Quote
 
Qun Cao
Guest
Posts: n/a
 
      10-17-2005
Thanks Neil, that's very useful to know.

Qun

 
Reply With Quote
 
Tim Peters
Guest
Posts: n/a
 
      10-18-2005
[Qun Cao]
>> import thread
>> def main():
>> thread.start_new(test.())
>>
>> def test():
>> print 'hello'
>>
>> main()
>> "
>> this program doesn't print out 'hello' as it is supposed to do.
>> while if I change main()


[Neil Hodgson]
> The program has exited before the thread has managed to run. It is
> undefined behaviour whether secondary threads survive main thread
> termination but it looks like they don't on your system.


In fact, they don't on most systems.

> Use the threading module and call join to wait for all threads to
> complete before exiting the program.


That's a different story: threads from the `thread` module have
entirely OS-specific behavior when Python shuts down. Python knows a
lot more about threads from the newer `threading` module, & uses an
atexit() hook to ensure that the Python interpreter does _not_ go away
while a threading.Thread is still running(*). IOW, Python does the
join() for you for threading.Thread threads -- there's no need to do
it yourself.

(*) Unless you explicitly mark it as a daemon thread.
 
Reply With Quote
 
dcrespo
Guest
Posts: n/a
 
      10-18-2005
> Hello,

> I am just starting to play threading in python, here is a really
> interesting problem I am very curious about:
> "
> import thread
> def main():
> thread.start_new(test.())


First, delete the dot after "test".
Second, is possibly that the Main() finishes before you can see the
print out of test().

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd problem with python sockets and threading thrthr@gmail.com Python 0 10-20-2008 06:17 PM
Wierd Map Behavior subaruwrx88011 C++ 5 02-20-2006 10:06 PM
Wierd behavior with text box postback =?Utf-8?B?TWljaGFlbCBMb3VnaHJ5?= ASP .Net 3 06-22-2005 02:15 PM
Wierd datalist behavior Steve ASP .Net 0 07-08-2003 04:43 PM
Wierd Behavior of __doPostBack paul reed ASP .Net 1 07-08-2003 04:26 PM



Advertisments