开发者

Trapping signals in Python

开发者 https://www.devze.com 2022-12-10 17:44 出处:网络
According to the documentation: 开发者_开发百科 There is no way to “block” signals temporarily from critical sections (since this is not supported by all Unix flavors).

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?


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. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号