signalfd for Python
This is a cffi based module for Python that makes available the
signalfd() system call, as well as all of
signalfd() calls allows your code to receive signals via a file
descriptor, rather than via the normal asynchronous delivery method,
which makes signals much more compatible with event based solutions
(such as those using
select.poll, and so forth).
For details, please see the
signalfd(2) man page.
You will need the cffi module for Python.
import sys import signal import select from signalfd import signalfd, sigset # Create a signal set containing all signals. mask = sigset() mask.fill() with signalfd(mask) as fd: poll = select.poll() poll.register(fd, select.POLLIN) poll.register(sys.stdin, select.POLLIN) # Print signals as they are received until user presses # <RETURN>. while True: events = dict(poll.poll()) if fd.fileno() in events: info = fd.info() print 'received signal %d' % info.ssi_signo if sys.stdin.fileno() in events: print 'all done' break
signalfd for Python Copyright (C) 2013 Lars Kellogg-Stedman firstname.lastname@example.org
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.