简体   繁体   English

带有Python的多处理池imap的KeyboardInterrupts

[英]KeyboardInterrupts with python's multiprocessing Pool imap

follow the some suggestions in this question Keyboard Interrupts with python's multiprocessing Pool , I write the test code named test.py 遵循此问题中的一些建议, 使用python的multiprocessing Pool进行键盘中断 ,我编写了名为test.py的测试代码

from multiprocessing import Pool
from multiprocessing.pool import IMapIterator
import fileinput

def wrapper(func):
  def wrap(self, timeout=None):
  # Note: the timeout of 1 googol seconds introduces a rather subtle
  # bug for Python scripts intended to run many times the age of the universe.
  return func(self, timeout=timeout if timeout is not None else 1e100)
return wrap
IMapIterator.next = wrapper(IMapIterator.next)

def main():
    p = Pool(1)
    try:
        for _ in p.imap(worker, fileinput.input(), 1):
            pass
    except KeyboardInterrupt:
        p.terminate()

def worker(line):
    print line

if __name__ == '__main__':
    main()

when I run python test.py in terminal: 当我在终端中运行python test.py时:

python test.py

I still got the KeyboardInterrupt traceback, furthermore I have to send Ctrl^C two times to stop the script. 我仍然得到了KeyboardInterrupt的追溯,此外,我还必须发送两次Ctrl ^ C来停止脚本。 How Can I handle KeyboardInterrupt and let it not print the tarceback and just use one time Ctrl^c to stop it gracefully. 我该如何处理KeyboardInterrupt并使其不打印后退,而只使用一次Ctrl ^ c即可正常停止它。

^CProcess PoolWorker-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 102, in worker
    task = get()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/queues.py", line 376, in get
    return recv()
KeyboardInterrupt
^CTraceback (most recent call last):
File "test.py", line 27, in <module>
    main()
File "test.py", line 21, in main
    p.terminate()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 452, in terminate
    self._worker_handler._state = TERMINATE
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/util.py", line 201, in __call__
    res = self._callback(*self._args, **self._kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 504, in _terminate_pool
    if threading.current_thread() is not task_handler:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 958, in join
    self.__block.wait(delay)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 358, in wait
    _sleep(delay)
KeyboardInterrupt

Look at signal module included in standard library. 查看标准库中包含的信号模块。 You can register signal handler in main process 您可以在主进程中注册信号处理程序

from signal import *

def siginthndlr(sig, frame):
    '''do what you need here'''
    print "Keyboard interrupt catched"

signal(SIGINT, siginthndlr) #Register SIGINT handler function

that would gracefully kill worker processes and than kill main process. 那会优雅地杀死工作进程,而不是杀死主进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM