简体   繁体   中英

Trapping signals in Python

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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