Last active
June 21, 2024 18:48
-
-
Save sric0880/376c536daa41792dc142df09706e17db to your computer and use it in GitHub Desktop.
interrupt or sigterm multiprocessing pool gracefully
This file contains hidden or 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 os | |
| import sys | |
| import time | |
| import signal | |
| from multiprocessing import Pool | |
| def int_handler(signum, frame): | |
| print('(%s) int_handler' % os.getpid()) | |
| raise KeyboardInterrupt() | |
| def worker(x): | |
| try: | |
| while True: | |
| time.sleep(1) | |
| print('(%s) loop' % os.getpid()) | |
| except KeyboardInterrupt: | |
| print('(%s) worker exception' % os.getpid()) | |
| sys.exit(0) | |
| def main(args): | |
| print('(%s) main' % os.getpid()) | |
| signal.signal(signal.SIGTERM, int_handler) | |
| signal.signal(signal.SIGINT, int_handler) | |
| pool = Pool(processes=4) | |
| try: | |
| results = pool.map_async(worker, range(4)) # 必须用异步接口,否者主进程被阻塞,不会执行int_handler | |
| while not results.ready(): | |
| results.wait(0.1) | |
| lst = results.get() | |
| except KeyboardInterrupt: | |
| print('(%s) main exception' % os.getpid()) | |
| pool.close() | |
| pool.terminate() | |
| print('(%s) terminate' % os.getpid()) | |
| except Exception as e: | |
| pool.close() | |
| pool.terminate() | |
| print('pool is terminated') | |
| if __name__ == '__main__': | |
| main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment