[Python-Dev] Signals+Threads (PyGTK waking up 10x/sec).
Guido van Rossum
guido at python.org
Sat Dec 8 18:20:06 CET 2007
On Dec 8, 2007 3:58 AM, Gustavo Carneiro <gjcarneiro at gmail.com> wrote:
> Not only that, but pygtk is a generic module;
What does "generic" mean in this context? Surely not that it applies
to other libraries than GTK?
> who are we to forbid the usage
> of signals if python itself allows it? If we were to ignore signals then
> sooner or later someone would come along and shout "hey, signals work just
> fine in pure python, so why did pygtk have to break my signals?".
Um, signals don't "work just fine" in pure Python. And I would argue
they don't either in C. They are extremely subtle, and most code using
signals is broken in some way. Just try to read the sigaction() man
page and claim you understand it.
Unfortunately, in Unix there are some conditions that can only be
delivered using signals (e.g. SIGINTR, SIGTERM) and others for which
your choices are either polling or signals (SIGCHILD, SIGWINCH).
Traditionally, solutions based on select() or poll() with a short
timeout (e.g. 20 or 100 ms) have worked well, as the number of
instructions executed each time is really negligeable, and the
response time is still reasonable on a human time scale. Unfortunately
it seems recent developments in power management for ultra-low power
devices have made this an issue again.
Windows solves this more elegantly by having a unified "wait for
multiple conditions" system call which can wait on I/O, semaphores,
and other events (within the same process or coming from other
processes). Unfortunately, in Unix, some events don't have a file
descriptor associated with them, and for those select()/poll() cannot
The best solution I can think of is to add a new API that takes a
signal and a file descriptor and registers a C-level handler for that
signal which writes a byte to the file descriptor. You can then create
a pipe, connect the signal handler to the write end, and add the read
end to your list of file descriptors passed to select() or poll(). The
handler must be written in C in order to avoid the race condition
referred to by Glyph (signals arriving after the signal check in the
VM main loop but before the select()/poll() system call is entered
will not be noticed until the select()/poll() call completes).
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev