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.

Author serhiy.storchaka
Recipients asvetlov, bquinlan, erickpeirson, pitrou, serhiy.storchaka, yselivanov
Date 2021-10-04.16:06:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1633363588.53.0.0448052267584.issue45339@roundup.psfhosted.org>
In-reply-to
Content
You can call __enter__ in the initializer. Seems there is no easy way to call __exit__ at shutting down the thread, perhaps something like per-thread atexit could help. As an example, you can save the context manager in thread locals and manually repeatedly submit a function that calls __exit__ and blocks the thread on some Barrier before calling ThreadPoolExecutor.shutdown(). It is complicated, so we may add some helpers to support context managers.

What are examples of your some_important_context()? Is it important to call some code before destroying the thread?

The problem with allowing the user to specify the Thread subclass is that in general using Thread is an implementation detail. ThreadPoolExecutor could be implemented using the low-level _thread module instead. Or in future it can need to create a special Thread subclass, and user-specified Thread subclass can be not compatible with it. It is safer to limit ways in which the user can affect execution. The initializer parameter was added to address cases similar to your.

Note also that ThreadPoolExecutor and ProcessPoolExecutor have almost identical interface. If we add some feature in ThreadPoolExecutor we will have a pressure to add the same feature in ProcessPoolExecutor to solve similar problems.
History
Date User Action Args
2021-10-04 16:06:28serhiy.storchakasetrecipients: + serhiy.storchaka, bquinlan, pitrou, asvetlov, yselivanov, erickpeirson
2021-10-04 16:06:28serhiy.storchakasetmessageid: <1633363588.53.0.0448052267584.issue45339@roundup.psfhosted.org>
2021-10-04 16:06:28serhiy.storchakalinkissue45339 messages
2021-10-04 16:06:28serhiy.storchakacreate