0
我使用主管來運行這樣的腳本。所以當主管停下來或中斷時,我試着從腳本中正常退出。這是我當前的代碼Python進程池執行程序關閉信號
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(等待=真/假)。所以我試了一下,我無法讓執行器關閉(它只是掛起)。請幫我帶着這些疑問
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.
如果可以做得更好,請添加您的意見。 – Anandan