pebble icon indicating copy to clipboard operation
pebble copied to clipboard

Feature Request: Finaliser Method

Open liam-veitch opened this issue 1 year ago • 1 comments

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!!

liam-veitch avatar Jul 27 '22 17:07 liam-veitch

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 avatar Jul 27 '22 20:07 noxdafox

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

noxdafox avatar Sep 04 '22 10:09 noxdafox