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?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'thread': 0.14; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'instead:': 0.16; 'iterating': 0.16; 'optionally': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:threads': 0.16; 'timeout': 0.16; 'wrote:': 0.18; 'stefan': 0.19; 'header:User- Agent:1': 0.23; 'stick': 0.24; 'pass': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'appreciated.': 0.29; 'related': 0.29; "d'aprano": 0.31; 'steven': 0.31; 'probably': 0.32; 'url:python': 0.33; 'tool': 0.35; 'received:84': 0.35; 'but': 0.35; 'choosing': 0.36; 'in.': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'question,': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'url:3': 0.61; 'finish': 0.65; 'results': 0.69; 'done:': 0.84; 'received:arcor-ip.net': 0.84; 'received:pools .arcor-ip.net': 0.84; 'subject:being': 0.84; 'subject:check': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: How to check for threads being finished?
Date Sat, 06 Jul 2013 10:49:00 +0200
References <51d6fb8a$0$29999$c3e8da3$5496439d@news.astraweb.com> <51d6fe8b$0$15956$e4fe514c@news.xs4all.nl>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host dslb-084-056-028-169.pools.arcor-ip.net
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7
In-Reply-To <51d6fe8b$0$15956$e4fe514c@news.xs4all.nl>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4331.1373100553.3114.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373100553 news.xs4all.nl 15967 [2001:888:2000:d::a6]:44396
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50055

Show key headers only | 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