Last active
June 12, 2019 08:29
-
-
Save MrQianJinSi/2daf5b6a9ef08b00facdfbea5200dd28 to your computer and use it in GitHub Desktop.
do cleanup work when sub-process exit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: https://github.com/python/cpython/blob/master/Lib/multiprocessing/util.py#L147 | |
Finalize(None, finalizer, exitpriority=1) | |
# atexit module don't work along with multiprocessing module | |
# because sub-process exit via os._exit() | |
# ref: https://docs.python.org/3/library/atexit.html | |
# atexit.register(finalizer) # don't work | |
print('main process ID: {}'.format(os.getpid())) | |
with mp.Pool(4, initializer=initializer) as executor: | |
executor.map(worker, range(20)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment