Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50055

Re: How to check for threads being finished?

From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: How to check for threads being finished?
Date 2013-07-06 10:49 +0200
References <51d6fb8a$0$29999$c3e8da3$5496439d@news.astraweb.com> <51d6fe8b$0$15956$e4fe514c@news.xs4all.nl>
Newsgroups comp.lang.python
Message-ID <mailman.4331.1373100553.3114.python-list@python.org> (permalink)

Show all headers | View raw


Irmen de Jong, 05.07.2013 19:12:
> On 5-7-2013 18:59, Steven D'Aprano wrote:
>> I then block until the threads are all done:
>>
>> while any(t.isAlive() for t in threads):
>>     pass
>>
>>
>> Is that the right way to wait for the threads to be done? Should I stick 
>> a call to time.sleep() inside the while loop? If so, how long should I 
>> sleep? That's probably an unanswerable question, but some guidelines on 
>> choosing the sleep time will be appreciated.
>>
> 
> I think your while loop busy-waits until the threads are completed.
> Do this instead:
> 
> for t in threads: t.join()    # optionally pass a timeout to join

A related stdlib tool that many people don't seem to know is the thread
pool in concurrent.futures. It supports both waiting for all threads to
finish as well as iterating over results as they come in. It also comes
with a parallel map() implementation.

http://docs.python.org/3/library/concurrent.futures.html#threadpoolexecutor-example

http://docs.python.org/3/library/concurrent.futures.html#module-functions

New in Py3.2, but there's also a backport AFAIR.

Stefan

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to check for threads being finished? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 16:59 +0000
  Re: How to check for threads being finished? Chris Angelico <rosuav@gmail.com> - 2013-07-06 03:11 +1000
  Re: How to check for threads being finished? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2013-07-05 19:12 +0200
    Re: How to check for threads being finished? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-06 02:18 +0000
    Re: How to check for threads being finished? Stefan Behnel <stefan_ml@behnel.de> - 2013-07-06 10:49 +0200
  Re: How to check for threads being finished? Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-05 11:18 -0600
  Re: How to check for threads being finished? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-07-05 16:13 -0400
  Re: How to check for threads being finished? Cameron Simpson <cs@zip.com.au> - 2013-07-06 10:45 +1000

csiph-web