Threading question

Discussion in 'Python' started by Torsten Marek, Apr 15, 2004.

  1. Hello to all,

    I have a simple question about threads in Python. I am starting a
    number of threads in my program and I want print out the amount of time
    needed after all threads stopped. Do I need to explictly join the
    threads, set a condition or is it possible to somehow work with
    try/finally like that:

    try:
    for i in range(0, num_threads):
    s = MyThread()
    s.start()
    finally:
    print "all threads finished"


    Thanks in advance

    Torsten
    Torsten Marek, Apr 15, 2004
    #1
    1. Advertising

  2. Torsten Marek

    Peter Hansen Guest

    Torsten Marek wrote:

    > I have a simple question about threads in Python. I am starting a
    > number of threads in my program and I want print out the amount of time
    > needed after all threads stopped. Do I need to explictly join the
    > threads, set a condition or is it possible to somehow work with
    > try/finally like that:
    >
    > try:
    > for i in range(0, num_threads):
    > s = MyThread()
    > s.start()
    > finally:
    > print "all threads finished"


    The other threads are running in, well, other threads, so you
    won't see any effect on the above thread when they end. Therefore
    you can't get much mileage out of a finally clause. A .join()
    is definitely the way to go here.

    -Peter
    Peter Hansen, Apr 15, 2004
    #2
    1. Advertising

  3. Peter Hansen schrieb:
    > Torsten Marek wrote:
    >
    >> I have a simple question about threads in Python. I am starting a
    >> number of threads in my program and I want print out the amount of
    >> time needed after all threads stopped. Do I need to explictly join the
    >> threads, set a condition or is it possible to somehow work with
    >> try/finally like that:
    >>
    >> try:
    >> for i in range(0, num_threads):
    >> s = MyThread()
    >> s.start()
    >> finally:
    >> print "all threads finished"

    >
    >
    > The other threads are running in, well, other threads, so you
    > won't see any effect on the above thread when they end. Therefore
    > you can't get much mileage out of a finally clause. A .join()
    > is definitely the way to go here.
    >
    > -Peter

    I found another way to go for now, which works fine for me.
    Since the threading module sets sys.exitfunc, I just do:

    def my_exitfunc():
    global thread_wait
    thread_wait()
    # own stuff follows here...

    thread_wait = sys.exitfunc
    sys.exitfunc = my_exitfunc

    That's maybe not very clean, but it's just a minor script I wrote, so I
    don't want to waste to much time on it;-)

    Anyway, thanks for the answer

    Torsten
    Torsten Marek, Apr 15, 2004
    #3
  4. Torsten Marek

    Peter Hansen Guest

    Torsten Marek wrote:

    > Peter Hansen schrieb:
    >> The other threads are running in, well, other threads, so you
    >> won't see any effect on the above thread when they end. Therefore
    >> you can't get much mileage out of a finally clause. A .join()
    >> is definitely the way to go here.


    > I found another way to go for now, which works fine for me.
    > Since the threading module sets sys.exitfunc, I just do:
    >
    > def my_exitfunc():
    > global thread_wait
    > thread_wait()
    > # own stuff follows here...
    >
    > thread_wait = sys.exitfunc
    > sys.exitfunc = my_exitfunc
    >
    > That's maybe not very clean, but it's just a minor script I wrote, so I
    > don't want to waste to much time on it;-)


    Good solution. You're actually taking advantage of the fact that
    the main thread does a join() on all non-daemon threads automatically
    as it exits. The source in threading.py for the "main thread"
    can make illuminating reading...

    -Peter
    Peter Hansen, Apr 15, 2004
    #4
    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. Alina
    Replies:
    0
    Views:
    1,671
    Alina
    Jul 16, 2003
  2. John

    Threading question

    John, Sep 28, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    628
    =?Utf-8?B?RGF2ZSBCYWNoZXI=?=
    Oct 7, 2004
  3. Replies:
    9
    Views:
    1,015
    Mark Space
    Dec 29, 2007
  4. Steven Woody
    Replies:
    0
    Views:
    394
    Steven Woody
    Jan 9, 2009
  5. Steven Woody
    Replies:
    0
    Views:
    436
    Steven Woody
    Jan 9, 2009
Loading...

Share This Page