Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
do cleanup work when sub-process exit
import multiprocessing as mp
from multiprocessing.util import Finalize
import os
import time
def finalizer():
time.sleep(0.2) # some time consuming work
print('do cleanup work: {}'.format(os.getpid()))
def worker(_):
print('do some work: {}'.format(os.getpid()))
def initializer():
# ref:
Finalize(None, finalizer, exitpriority=1)
# atexit module don't work along with multiprocessing module
# because sub-process exit via os._exit()
# ref:
# atexit.register(finalizer) # don't work
print('main process ID: {}'.format(os.getpid()))
with mp.Pool(4, initializer=initializer) as executor:, range(20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment