Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:

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.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue45339>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to