This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Ability to join() threads in concurrent.futures.ThreadPoolExecutor
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bquinlan, dktrkranz, josh.r
Priority: normal Keywords:

Created on 2014-09-08 10:29 by dktrkranz, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg226569 - (view) Author: Luca Falavigna (dktrkranz) Date: 2014-09-08 10:29
I have a program which waits for external events (mostly pyinotify events), and when events occur a new worker is created using concurrent.futures.ThreadPoolExecutor. The following snippet represents shortly what my program does:

from time import sleep
from concurrent.futures import ThreadPoolExecutor

def func():
    print("start")
    sleep(10)
    print("stop")

ex = ThreadPoolExecutor(1)

# New workers will be scheduled when an event
# is triggered (i.e. pyinotify events)
ex.submit(func)

# Dummy sleep
sleep(60)

When func() is complete, I'd like the underlying thread to be terminated. I realize I could call ex.shutdown() to achieve this, but this would prevent me from adding new workers in case new events occur. Not calling ex.shutdown() leads to have unfinished threads which pile up considerably:

(gdb) run test.py
Starting program: /usr/bin/python3.4-dbg test.py
[Thread debugging using libthread_db enabled]
[New Thread 0x7ffff688e700 (LWP 17502)]
start
stop
^C
Program received signal SIGINT, Interrupt.
0x00007ffff6e41963 in select () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) info threads
  Id   Target Id         Frame
  2    Thread 0x7ffff688e700 (LWP 17502) "python3.4-dbg" 0x00007ffff7bce420 in sem_wait () from /lib/x86_64-linux-gnu/libpthread.so.0
* 1    Thread 0x7ffff7ff1700 (LWP 17501) "python3.4-dbg" 0x00007ffff6e41963 in select () from /lib/x86_64-linux-gnu/libc.so.6
(gdb)

Would it be possible to add a new method (or a ThreadPoolExecutor option) which allows to join the underlying thread when the worker function returns?
msg226615 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2014-09-08 21:54
Can you explain what benefit this would provide? Forcing the thread to exit gets you relatively little benefit. If it's an infrequently used executor, I suppose you avoid the cost of leaving worker threads blocked waiting for work, but that cost is tiny, and you pay for it with increased overhead to dispatch new tasks since they have to create new threads instead of using existing worker threads.
msg226629 - (view) Author: Luca Falavigna (dktrkranz) Date: 2014-09-09 08:35
There is indeed little benefit in freeing up resources left open by a unused thread, but it could be worth closing it for specific needs (e.g. thread processes sensible information) or in embedded systems with very low resources.
msg341629 - (view) Author: Brian Quinlan (bquinlan) * (Python committer) Date: 2019-05-06 19:52
So you actually use the result of ex.submit i.e. use the resulting future?

If you don't then it might be easier to just create your own thread.
History
Date User Action Args
2022-04-11 14:58:07adminsetgithub: 66557
2019-05-06 19:52:27bquinlansetmessages: + msg341629
2014-09-09 08:35:47dktrkranzsetmessages: + msg226629
2014-09-09 02:30:19ned.deilysetnosy: + bquinlan
2014-09-08 21:54:38josh.rsetnosy: + josh.r
messages: + msg226615
2014-09-08 10:29:45dktrkranzcreate