Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在IPython中捕获SIGTSTP_Python_Signals_Ipython - Fatal编程技术网

在IPython中捕获SIGTSTP

在IPython中捕获SIGTSTP,python,signals,ipython,Python,Signals,Ipython,为SIGTSTP注册信号处理程序在普通Python解释器中工作 (py3)$ python Python 3.4.3 |Continuum Analytics, Inc.| (default, Mar 6 2015, 12:07:41) [GCC 4.2.1 (Apple Inc. build 5577)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>

为SIGTSTP注册信号处理程序在普通Python解释器中工作

(py3)$ python
Python 3.4.3 |Continuum Analytics, Inc.| (default, Mar  6 2015, 12:07:41) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> def handler(x, frame):
...     print("CAUGHT", x)
... 
>>> signal.signal(signal.SIGTSTP, handler)
0
现在我按了几次Ctrl+Z,它就如预期的那样工作了

>>> CAUGHT 18
CAUGHT 18
CAUGHT 18
但是,在IPython,它不起作用。新的处理程序被调用,但原始的处理程序似乎也被调用,并且IPython被停止

IPython 3.2.0 -- An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: def handler(x, frame):
    print("CAUGHT", x)
   ...:     

In [2]: import signal

In [3]: signal.signal(signal.SIGTSTP, handler)
Out[3]: 0
现在我按Ctrl+Z键:

In [4]: CAUGHT 18

[2]+  Stopped                 ipython
顺便说一句,我看不出Ctrl+C有什么问题:

In [5]: signal.signal(signal.SIGINT, handler)
Out[5]: <function signal.default_int_handler>

In [6]: CAUGHT 2
[5]中的
:signal.signal(signal.SIGINT,handler)
出[5]:
在[6]中:捕获2

IPython对SIGTSTP做了什么?奇怪的是,我在IPython 3.2代码库中找不到任何字符串“SIGTSTP”的出现。

关于
signal.signal()
唯一可靠的说法是,它的行为是非常特定于系统的,它使用了旧的signal(2)机制,而sigaction(2)已经被弃用因为它的主要缺陷。我怀疑IPython的多线程设计与明显的多接收器有很大关系。我觉得这两种行为显然都不正确。在我的linux系统上,ipython3和python3都使用代码将SIGTSTP视为终端错误。IPython不太使用线程(只有一个额外的线程,用于写入历史数据库)。信号模块应确保信号正常。我很确定我们没有设置任何特殊的信号处理程序(除了在进行gui集成时使用sigint的一些东西)。也许这与我们对readline的使用有关?关于
signal.signal()
唯一可靠的说法是,它的行为是非常特定于系统的,并且它使用了旧的信号(2)机制,由于其主要缺陷,该机制已被弃用,取而代之的是sigaction(2)。我怀疑IPython的多线程设计与明显的多接收器有很大关系。我觉得这两种行为显然都不正确。在我的linux系统上,ipython3和python3都使用代码将SIGTSTP视为终端错误。IPython不太使用线程(只有一个额外的线程,用于写入历史数据库)。信号模块应确保信号正常。我很确定我们没有设置任何特殊的信号处理程序(除了在进行gui集成时使用sigint的一些东西)。也许这与我们使用readline有关?