how do you implement a reactor without a select?

sjdevnull at yahoo.com sjdevnull at yahoo.com
Mon May 7 13:36:14 EDT 2007


Michele Simionato wrote:
> Notice that I copied the Twisted terminology, but
> I did not look at Twisted implementation because I did not want to
> use a select (I assume that the GUI mainloops do not use it either).
> The trick I use is to store the actions to perform (which are
> callables identified by an integer) in an event dictionary and
> to run them in the mainlooop if the current time is greater than
> the scheduled time.
> I had to add a time.sleep(.001) call in the default_action to avoid
> consuming 100%
> of the CPU in the loop.

Busy-looping like that is ugly and inefficient, even with the sleep
thrown in.

Most GUI main loops _do_ use either select() or poll().  When Xt/GTK/
Qt/etc have function like "gtk_add_input" which takes an fd that
you'll get notified about if it's written to while you're in the main
loop, that's just adding another fd to the select() loop.

There are other ways to wait for events on an fd, but they tend to be
less portable.  Depending on your Unix flavor, epoll, /dev/poll,
kqueues, kevent, queued realtime signals, or something else might be
available from the OS (but probably not from Python without futzing
with ctypes or writing an extension).  If you want details, check out
http://www.kegel.com/c10k.html

The alternatives usually aren't really faster unless you have hundreds
of connections, though--select/poll have major portability advantages,
so go with them unless you have a compelling reason.




More information about the Python-list mailing list