noxdafox / pebble

Multi threading and processing eye-candy.
GNU Lesser General Public License v3.0
536 stars 52 forks source link

Feature Request: Finaliser Method #101

Closed liam-veitch closed 2 years ago

liam-veitch commented 2 years ago

The initialiser method is useful for calling at process creation. Would be great if there was an equivalent finalizer method to be called just prior to closing a worker process.

An example use case - I have a worker processes which must operate within their own directory (I call os.chdir). The folders are generated using a random string. The finalizer method would be used to delete these folders.

I use Python 2.7 - not everyone has the luxury of moving to higher versions due to interfacing with (old) commercial software!!

noxdafox commented 2 years ago

It is not possible to guarantee that a "finalizer" function is executed. For example, if the process is terminated upon timeout or future.cancel() request, it is likely that the function won't be called. The same would apply in case of pool termination as well as other cases (process OOM etc.).

This is why the pool does not provide a mirror for the initializer function.

You can easily implement the functionality yourself via atexit.register or sys.exitfunc. Keep in mind that none of these methods provide guarantees your cleanup function will be executed.

Example:

def initializer():
    path = tempfile.mkdtemp('/tmp/')
    os.chdir(path)
    atexit.register(cleanup, path)

def cleanup(path):
    os.rmdir(path)

pool = pebble.Pool(initializer=initializer)

...
noxdafox commented 2 years ago

Closing this issue due to lack of response. Please re-open if you require further clarifications.