Python 进程池执行器收到信号关闭

Python Process pool executor shutdown on signal

我使用 supervisor 来 运行 一些像这样的脚本。因此,当主管停止或中断时,我会尝试从脚本中优雅地退出。这是我当前的代码

import concurrent.futures
import random
import os
import signal
import sys

executor = concurrent.futures.ProcessPoolExecutor(max_workers=2)

some_flag = True

def some_func():
    while some_flag:
        executor.submit(print_some_num)

def print_some_num():
    print("From worker :{}".format(os.getpid()))
    print(random.randint(0,10))

def handler(arg1, arg2):
    print("Got interrupt")
    some_flag = False
    sys.exit("Run for ur life")
    #executor.shutdown(wait = False) ----> not working
    print("Shutdown")

signal.signal(signal.SIGTERM,handler)
signal.signal(signal.SIGINT,handler)

if __name__ == '__main__':
    some_func()

这很好用,现在当我读到 executor.shutdown(wait=True/False) 时我很困惑。所以我试过了,但我无法让执行程序关闭(它只是挂起)。请帮我解决这些问题

 1) what does executor.shutdown do that sys.exit() doesn't do.
 2) What is advisable in production environment? If executor shutdown is required, please help me fix the issue with shutdown. 

1) 显然,关机会停止所有正在处理的现有消息,但 sys.exit() 刚刚出来。 (在文档中 https://docs.python.org/3/library/concurrent.futures.html

2) 如果有人卡在同一个地方,使用一些全局变量来发出关机信号。

def some_func():
with concurrent.futures.ProcessPoolExecutor(max_workers=2) as executor:
    while True:
        global some_flag
        print(some_flag)
        if not some_flag:
            executor.shutdown(wait=True)
            sys.exit()
        else:
            executor.submit(print_some_num)

def print_some_num():
    print("From worker :{}".format(os.getpid()))
    print(random.randint(0,10))

def handler(arg1, arg2):
    global some_flag
    print("Got interrupt")
    some_flag = False
    print("Shutdown")

signal.signal(signal.SIGTERM,handler)
signal.signal(signal.SIGINT,handler)