Trapping signals in Python

微笑、不失礼 提交于 2020-01-02 08:22:46

问题


According to the documentation:

There is no way to “block” signals temporarily from critical sections (since this is not supported by all Unix flavors).

What stops me using signal.signal(signum,SIG_IGN) to block it, then adding the signal back?


回答1:


What stops you is that, if the signal actually arrives while SIG_IGN is in place, then it will be ignored and thrown away. When you add the signal back later, it's too late because it's gone and you'll never get to learn that it happened.

Thus, you will have "ignored" (= thrown away) the signal rather than "blocked" it (= kept it for handling at the end of the critical section). Your confusion here might just arise from not knowing specifically what "blocking" a signal means: it means the OS hangs onto the signal, letting it wait to strike until your critical section is complete.

See (as a great reference for all sorts of questions like this) W. Richard Steven's Advanced Programming in the UNIX Environment. Section 10.8 in the edition I have, "Reliable Signal Terminology and Semantics", is the one I just checked before answering to be sure of my answer.

Update: on my Ubuntu laptop, "man sigprocmask" (if manpages-dev is installed) seems to be the man page to start with for learning about signal blocking. Again, as the Python docs note, this isn't available under all Unixes, so don't expect your old Irix or AIX box to run your Python program if you actually use "sigprocmask". But maybe you're not worried about that. :-)



来源:https://stackoverflow.com/questions/1654215/trapping-signals-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!