Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50003 > unrolled thread
| Started by | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| First post | 2013-07-05 16:59 +0000 |
| Last post | 2013-07-06 10:45 +1000 |
| Articles | 8 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-07-05 16:59 +0000 |
| Subject | How to check for threads being finished? |
| Message-ID | <51d6fb8a$0$29999$c3e8da3$5496439d@news.astraweb.com> |
I have a pool of worker threads, created like this:
threads = [MyThread(*args) for i in range(numthreads)]
for t in threads:
t.start()
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.
--
Steven
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-07-06 03:11 +1000 |
| Message-ID | <mailman.4304.1373044272.3114.python-list@python.org> |
| In reply to | #50003 |
On Sat, Jul 6, 2013 at 2:59 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > I then block until the threads are all done: > > while any(t.isAlive() for t in threads): > pass > Using the threading module, I assume. Is there any reason you can't simply join() each thread in succession? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Irmen de Jong <irmen.NOSPAM@xs4all.nl> |
|---|---|
| Date | 2013-07-05 19:12 +0200 |
| Message-ID | <51d6fe8b$0$15956$e4fe514c@news.xs4all.nl> |
| In reply to | #50003 |
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 -Irmen
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-07-06 02:18 +0000 |
| Message-ID | <51d77e6e$0$29999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #50005 |
On Fri, 05 Jul 2013 19:12:44 +0200, Irmen de Jong wrote: > 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 Yes, the busy-wait was what I was concerned about. Thanks to everyone who suggested the same solution. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2013-07-06 10:49 +0200 |
| Message-ID | <mailman.4331.1373100553.3114.python-list@python.org> |
| In reply to | #50005 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-07-05 11:18 -0600 |
| Message-ID | <mailman.4305.1373044760.3114.python-list@python.org> |
| In reply to | #50003 |
On Fri, Jul 5, 2013 at 10:59 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> I have a pool of worker threads, created like this:
>
> threads = [MyThread(*args) for i in range(numthreads)]
> for t in threads:
> t.start()
>
>
> 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.
for thread in threads:
thread.join()
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-07-05 16:13 -0400 |
| Message-ID | <mailman.4312.1373055237.3114.python-list@python.org> |
| In reply to | #50003 |
On 05 Jul 2013 16:59:54 GMT, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> declaimed the following:
>I have a pool of worker threads, created like this:
>
>threads = [MyThread(*args) for i in range(numthreads)]
>for t in threads:
> t.start()
>
>
>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.
If you are just going to wait for them to finish, and never use them
again...
for t in threads:
t.join()
Doesn't matter which thread ends first, this operation will either
block if the thread hasn't finished or cycle to the next thread if it has.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Date | 2013-07-06 10:45 +1000 |
| Message-ID | <mailman.4317.1373071518.3114.python-list@python.org> |
| In reply to | #50003 |
On 05Jul2013 16:59, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
| I have a pool of worker threads, created like this:
|
| threads = [MyThread(*args) for i in range(numthreads)]
| for t in threads:
| t.start()
|
| I then block until the threads are all done:
|
| while any(t.isAlive() for t in threads):
| pass
This spins.
How about:
for t in threads:
t.join()
Blocks instead of polls.
Cheers,
--
Cameron Simpson <cs@zip.com.au>
Processes are like potatoes. - NCR device driver manual
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web