Hi, I thought maybe a callback before the worker of a Executor finishes would 
be helpful. It would allow the setup and teardown resources (such as file, 
databases, connections, ...) across workers. My particular use case is for 
closing down requests.Session objects, that currently its very complicated to 
cleanup properly. There are many other use cases, a generic format would be 
something like the following:

import threading
from concurrent.futures import ThreadPoolExecutor

worker_local = threading.local()

def session_initializer():
    # Initialize costly resources for a particular worker
    worker_local.resources = ...

def session_deinitializer():
    # Deinitialize worker resources carefully
    worker_local.resources ...

with ThreadPoolExecutor(initializer=session_initializer, 
deinitializer=session_deinitializer) as executor:
    executor.submit(task)
    ...
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BOKCVLL4B7COBVXFS5BPTTFTRFU3JKJD/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to