Python进程池执行器关闭信号

时间:2017-05-24 14:36:31

标签: python supervisor sys concurrent.futures

我使用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 个答案:

答案 0 :(得分:1)

1)显然,shutdown会停止正在处理的所有现有消息,但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)