467,073 Members | 1,262 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,073 developers. It's quick & easy.

wierd threading behavior

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
  • viewed: 1510
Share:
6 Replies
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
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
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
Thanks Neil, that's very useful to know.

Qun

Oct 17 '05 #5
[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
> 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.

Similar topics

19 posts views Thread by Jane Austine | last post: by
1 post views Thread by paul reed | last post: by
1 post views Thread by Sao Zagoranski | last post: by
4 posts views Thread by Muthu Arumugam | last post: by
10 posts views Thread by =?Utf-8?B?ZHNoZW1lc2g=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.