我将一些长时间运行的工作交给Pool并执行result.get():
result = my_pool.apply_async(long_run_func)
result.get()
此过程将被阻止,我尝试向此过程发送信号。然后我发现信号处理程序只会在result.get()完成后调用。
以下是我运行的示例代码:
import os
import sys
import atexit
import time
from threading import Thread
from multiprocessing import Pool
from signal import signal,SIGTERM,SIG_IGN
def func_in_pool():
for i in range(10):
print 'Sleeping... %d'%i
time.sleep(1)
def func_in_atexit():
print 'Calling from atexit()'
def func_in_thread():
print 'Calling from thread.'
time.sleep(4)
print 'Calling from thread: os.kill()'
os.kill(os.getpid(), SIGTERM)
def init_worker():
signal(SIGTERM, SIG_IGN)
my_pool=Pool(2, init_worker)
signal(SIGTERM, lambda signum, stack_frame: sys.exit(1))
atexit.register(func_in_atexit)
mythread = Thread(target=func_in_thread)
mythread.start()
print 'Thread has been started'
result = my_pool.apply_async(func_in_pool,[])
result.get()
time.sleep(2)
print 'After get()'
mythread.join()
我预计这个过程将在4s后结束,但实际上在10秒后结束,这意味着result.get()已经返回。
输出如下:
python myexit.py
Calling from thread.
Thread has been started
Sleeping... 0
Sleeping... 1
Sleeping... 2
Sleeping... 3
Calling from thread: os.kill()
Sleeping... 4
Sleeping... 5
Sleeping... 6
Sleeping... 7
Sleeping... 8
Sleeping... 9
Calling from atexit()
信号处理程序用于确保在atexit中注册的函数将被调用。我怎么能让后台线程生成一个信号,并且信号处理程序被异步调用?
(如果没有把信号处理程序放在那里,那么当线程生成信号时,进程将结束。但是,在这种情况下,在atexit中注册的函数不会被调用。)
答案 0 :(得分:1)
这是由Python中的bug引起的。解决方法是在get()
中指定超时 - 即使很长的时间也会执行:
result.get(timeout=525600*60) # It's time now, to sing out