我创建了一个程序,其中工作池!x
创建作业,该作业随后被馈送到回调方法senderpool
中的Connection Pipe
的一端。
在callbackfunc
进程内创建的另一个工作池receiverpool
正在ReceiverJobGenerator
的另一端馈送,等待所产生的工作并随后对其进行一些工作。
Connection Pipe
对于任何中断或其他系统信号,如何完成整个程序的清理?
在键盘上,中断程序无法正常停止并引发如下错误。
import datetime from functools import partial from multiprocessing import Process, Pipe, Pool import os def ReceiverJob(job): # Other processing steps print("Received {} from PID - {} at - {}".format(job, str(os.getpid()), str(datetime.datetime.now()))) return False def SenderJob(job): # Some processing steps print("Send {} from PID - {} at - {}".format(job, str(os.getpid()), str(datetime.datetime.now()))) return job def callbackfunc(job_stream, result): job_stream.send(result) print("callback for" + result) return result def ReceiverJobGenerator(job_stream_result): receiverpool = Pool(processes=2) print("ReceiverJobGenerator running") while True: try: if job_stream_result.poll(): sent = job_stream_result.recv() receiverpool.apply_async( ReceiverJob, args=([sent])) except: receiverpool.terminate() print("ReceiverJobGenerator terminates") def run(): parent_conn, child_conn = Pipe() senderpool = Pool(processes=3) callbackfuncpar = partial(callbackfunc, child_conn) for i in range(10): send_result = senderpool.apply_async(SenderJob, args=(["Job" + str(i)]), callback=callbackfuncpar) receiver_generator = Process(target=ReceiverJobGenerator, args=(parent_conn,)) try: receiver_generator.start() receiver_generator.join() except KeyboardInterrupt: senderpool.terminate() receiver_generator.terminate() print("Program terminates") if __name__=="__main__": run()