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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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