Closed liam-veitch closed 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)
...
Closing this issue due to lack of response. Please re-open if you require further clarifications.
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!!