wierd threading behavior

Discussion in 'Python' started by Qun Cao, Oct 15, 2005.

  1. Qun Cao

    Qun Cao Guest

    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
    Qun Cao, Oct 15, 2005
    #1
    1. Advertising

  2. Qun Cao

    Sam Pointon Guest

    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()
    Sam Pointon, Oct 15, 2005
    #2
    1. Advertising

  3. Qun Cao

    Qun Cao Guest

    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. :)
    Qun Cao, Oct 15, 2005
    #3
  4. Qun Cao

    Neil Hodgson Guest

    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
    Neil Hodgson, Oct 15, 2005
    #4
  5. Qun Cao

    Qun Cao Guest

    Thanks Neil, that's very useful to know.

    Qun
    Qun Cao, Oct 17, 2005
    #5
  6. Qun Cao

    Tim Peters Guest

    [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.
    Tim Peters, Oct 18, 2005
    #6
  7. Qun Cao

    dcrespo Guest

    > 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().
    dcrespo, Oct 18, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul reed

    Wierd Behavior of __doPostBack

    paul reed, Jul 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    557
    Bassel Tabbara [MSFT]
    Jul 8, 2003
  2. Steve

    Wierd datalist behavior

    Steve, Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    327
    Steve
    Jul 8, 2003
  3. =?Utf-8?B?TWljaGFlbCBMb3VnaHJ5?=

    Wierd behavior with text box postback

    =?Utf-8?B?TWljaGFlbCBMb3VnaHJ5?=, Jun 21, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    562
    =?Utf-8?B?TWljaGFlbCBMb3VnaHJ5?=
    Jun 22, 2005
  4. subaruwrx88011

    Wierd Map Behavior

    subaruwrx88011, Feb 20, 2006, in forum: C++
    Replies:
    5
    Views:
    373
    subaruwrx88011
    Feb 20, 2006
  5. Replies:
    0
    Views:
    290
Loading...

Share This Page