wierd threading behavior

Q

Qun Cao

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
 
S

Sam Pointon

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()
 
Q

Qun Cao

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. :)
 
N

Neil Hodgson

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
 
T

Tim Peters

[Qun Cao]
[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.
 
D

dcrespo

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().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top