By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,931 Members | 1,976 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,931 IT Pros & Developers. It's quick & easy.

wierd threading behavior

P: n/a
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

Oct 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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()

Oct 14 '05 #2

P: n/a
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. :)

Oct 14 '05 #3

P: n/a
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
Oct 15 '05 #4

P: n/a
Thanks Neil, that's very useful to know.

Qun

Oct 17 '05 #5

P: n/a
[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.
Oct 18 '05 #6

P: n/a
> 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().

Oct 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.