Threading Problem

Discussion in 'Python' started by Norbert, Dec 22, 2004.

  1. Norbert

    Norbert Guest

    Hello *,
    i am experimenting with threads and get puzzling results.
    Consider the following example:
    #--------------------
    import threading, time

    def threadfunction():
    .....print "threadfunction: entered"
    .....x = 10
    .....while x < 40:
    .........time.sleep(1) # time unit is seconds
    .........print "threadfunction x=%d" % x
    .........x += 10



    print "start"
    th = threading.Thread(target = threadfunction())
    th.start()
    print "start completed"
    #--------------------
    (the dots are inserted becaus Google mangles the lines otherwise)

    This program gives the following result :
    ----
    start
    threadfunction: entered
    threadfunction x=10
    threadfunction x=20
    threadfunction x=30
    start completed
    ----
    My aim was that the main program should continue to run while
    threadfunction runs in parallel. That's the point of threads after all,
    isn't it ?

    I awaited something like the following :
    start
    threadfunction: entered
    start completed <-------------------
    threadfunction x=10
    threadfunction x=20
    threadfunction x=30

    Does anyone know what's going on here ?

    Thanks for listening !

    Norbert
    Norbert, Dec 22, 2004
    #1
    1. Advertising

  2. Norbert

    Steve Holden Guest

    Norbert wrote:

    > Hello *,
    > i am experimenting with threads and get puzzling results.
    > Consider the following example:
    > #--------------------
    > import threading, time
    >
    > def threadfunction():
    > .....print "threadfunction: entered"
    > .....x = 10
    > .....while x < 40:
    > .........time.sleep(1) # time unit is seconds
    > .........print "threadfunction x=%d" % x
    > .........x += 10
    >
    >
    >
    > print "start"
    > th = threading.Thread(target = threadfunction())
    > th.start()
    > print "start completed"
    > #--------------------
    > (the dots are inserted becaus Google mangles the lines otherwise)
    >
    > This program gives the following result :
    > ----
    > start
    > threadfunction: entered
    > threadfunction x=10
    > threadfunction x=20
    > threadfunction x=30
    > start completed
    > ----
    > My aim was that the main program should continue to run while
    > threadfunction runs in parallel. That's the point of threads after all,
    > isn't it ?
    >
    > I awaited something like the following :
    > start
    > threadfunction: entered
    > start completed <-------------------
    > threadfunction x=10
    > threadfunction x=20
    > threadfunction x=30
    >
    > Does anyone know what's going on here ?
    >


    Well, I don't believe there's any guarantee that a thread will get run
    preference over its starter - they're both threads, after all. Try
    putting a sleep after th.start() and before the print statement and you
    should see that the "worker" thread runs while the main thread sleeps.

    The same would be true if each were making OS calls and so on. When
    everything is more or les continuous computation, and so short it can be
    over in microseconds, there's no motivation for the scheduler to stop
    one thread and start another.

    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
    Steve Holden, Dec 22, 2004
    #2
    1. Advertising

  3. Norbert

    Alan Kennedy Guest

    [Norbert]
    > i am experimenting with threads and get puzzling results.
    > Consider the following example:
    > #--------------------
    > import threading, time
    >
    > def threadfunction():
    > ....print "threadfunction: entered"
    > ....x = 10
    > ....while x < 40:
    > ........time.sleep(1) # time unit is seconds
    > ........print "threadfunction x=%d" % x
    > ........x += 10
    >
    >
    >
    > print "start"
    > th = threading.Thread(target = threadfunction())


    The problem is here ^^

    You are *invoking* threadfunction, and passing its return value as the
    target, rather than passing the function itself as the target. That's
    why threadfunction's output appears in the output stream before the
    thread has even started.

    Try this instead

    #-----------------------------------------------

    import threading, time

    def threadfunction():
    print "threadfunction: entered"
    x = 10
    while x < 40:
    time.sleep(1) # time unit is seconds
    print "threadfunction x=%d" % x
    x += 10

    print "start"
    th = threading.Thread(target = threadfunction)
    th.start()
    print "start completed"

    #------------------------------------------------

    Which should output the expected

    >

    start
    threadfunction: entered
    start completed
    threadfunction x=10
    threadfunction x=20
    threadfunction x=30

    regards,

    --
    alan kennedy
    ------------------------------------------------------
    email alan: http://xhaus.com/contact/alan
    Alan Kennedy, Dec 22, 2004
    #3
  4. Norbert

    Norbert Guest

    Thanks a lot, Steve, for your fast reply.
    But the behaviour is the same if 'threadfunction' sleeps longer than
    just 1 second. 'threadfunction' is of course a dummy to show the
    problem, imagine a longrunning background-task.

    If you are right, the question remains 'How can I assure that the
    starting function finishes, while the other thread still runs ?' . As
    I said, this is the purpose of threading.

    Thanks again
    Norbert
    Norbert, Dec 22, 2004
    #4
  5. Norbert

    Norbert Guest

    Thanks Alan,
    i hoped it would be something trivial :)

    Norbert
    Norbert, Dec 22, 2004
    #5
  6. Steve Holden wrote:

    > Well, I don't believe there's any guarantee that a thread will get run preference over its
    > starter - they're both threads, after all. Try putting a sleep after th.start() and before the
    > print statement and you should see that the "worker" thread runs while the main thread sleeps.


    that's correct, but the "threading" module does a 0.000001-second sleep
    to get around this, no matter what thread scheduler you're using.

    if you're building threads on top of the lower-level "thread" api, you have
    to do that yourself.

    </F>
    Fredrik Lundh, Dec 22, 2004
    #6
  7. Norbert

    Ishwor Guest

    On Wed, 22 Dec 2004 12:55:46 +0000, Alan Kennedy <> wrote:
    > [Norbert]
    > > i am experimenting with threads and get puzzling results.
    > > Consider the following example:
    > > #--------------------
    > > import threading, time
    > >
    > > def threadfunction():
    > > ....print "threadfunction: entered"
    > > ....x = 10
    > > ....while x < 40:
    > > ........time.sleep(1) # time unit is seconds
    > > ........print "threadfunction x=%d" % x
    > > ........x += 10
    > >
    > >
    > >
    > > print "start"
    > > th = threading.Thread(target = threadfunction())

    >
    > The problem is here ^^
    >
    > You are *invoking* threadfunction, and passing its return value as the
    > target, rather than passing the function itself as the target. That's
    > why threadfunction's output appears in the output stream before the
    > thread has even started.
    >
    > Try this instead
    >
    > #-----------------------------------------------
    >
    > import threading, time
    >
    > def threadfunction():
    > print "threadfunction: entered"
    > x = 10
    > while x < 40:
    > time.sleep(1) # time unit is seconds
    > print "threadfunction x=%d" % x
    > x += 10
    >
    > print "start"
    > th = threading.Thread(target = threadfunction)
    > th.start()
    > print "start completed"
    >
    > #------------------------------------------------
    >
    > Which should output the expected
    >
    > >

    > start
    > threadfunction: entered
    > start completed
    > threadfunction x=10
    > threadfunction x=20
    > threadfunction x=30
    >
    > regards,
    >
    > --
    > alan kennedy
    > ------------------------------------------------------
    > email alan: http://xhaus.com/contact/alan


    nice chum ;) liked the way you explained. Clear and easy to
    understand.. why doesn't the keyboardInterrupt seem to work? I am
    working in IDLE and it doesn't seem to stop the thread ???!!??
    output of above code in IDLE shell:----

    threadfunction x=75

    KeyboardInterrupt
    >>>

    threadfunction x=80



    --
    cheers,
    Ishwor Gurung
    Ishwor, Dec 22, 2004
    #7
  8. Norbert

    Ishwor Guest

    On Wed, 22 Dec 2004 12:55:46 +0000, Alan Kennedy <> wrote:
    > [Norbert]
    > > i am experimenting with threads and get puzzling results.
    > > Consider the following example:
    > > #--------------------
    > > import threading, time
    > >
    > > def threadfunction():
    > > ....print "threadfunction: entered"
    > > ....x = 10
    > > ....while x < 40:
    > > ........time.sleep(1) # time unit is seconds
    > > ........print "threadfunction x=%d" % x
    > > ........x += 10
    > >
    > >
    > >
    > > print "start"
    > > th = threading.Thread(target = threadfunction())

    >
    > The problem is here ^^
    >
    > You are *invoking* threadfunction, and passing its return value as the
    > target, rather than passing the function itself as the target. That's
    > why threadfunction's output appears in the output stream before the
    > thread has even started.
    >
    > Try this instead
    >
    > #-----------------------------------------------
    >
    > import threading, time
    >
    > def threadfunction():
    > print "threadfunction: entered"
    > x = 10
    > while x < 40:
    > time.sleep(1) # time unit is seconds
    > print "threadfunction x=%d" % x
    > x += 10
    >
    > print "start"
    > th = threading.Thread(target = threadfunction)
    > th.start()
    > print "start completed"
    >
    > #------------------------------------------------
    >
    > Which should output the expected
    >
    > >

    > start
    > threadfunction: entered
    > start completed
    > threadfunction x=10
    > threadfunction x=20
    > threadfunction x=30
    >
    > regards,
    >
    > --
    > alan kennedy
    > ------------------------------------------------------
    > email alan: http://xhaus.com/contact/alan


    nice chum ;) liked the way you explained. Clear and easy to
    understand.. why doesn't the keyboardInterrupt seem to work? I am
    working in IDLE and it doesn't seem to stop the thread ???!!??
    output of above code in IDLE shell:----

    threadfunction x=75

    KeyboardInterrupt
    >>>

    threadfunction x=80



    --
    cheers,
    Ishwor Gurung
    Ishwor, Dec 22, 2004
    #8
    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. Replies:
    9
    Views:
    1,030
    Mark Space
    Dec 29, 2007
  2. Steven Woody
    Replies:
    0
    Views:
    404
    Steven Woody
    Jan 9, 2009
  3. Steven Woody
    Replies:
    0
    Views:
    444
    Steven Woody
    Jan 9, 2009
  4. mk
    Replies:
    0
    Views:
    332
  5. MRAB
    Replies:
    0
    Views:
    401
Loading...

Share This Page