how do you implement a reactor without a select?

Diez B. Roggisch deets at nospam.web.de
Mon May 7 10:13:08 EDT 2007


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

Why do you assume that? It's a wrong assumption. Yielding a thread/process
until the OS wakes it up because of IO to be performed is the proper way to
go. And at least in unix, IO is _everything_, also mouse-movements and
keyboard events. Most probably the OS will have specialized APIs (or some
wrapper lib has) that allow for reactor registration for events of
different kinds including timers. But basically, it's select - I mean you
could easily offer a timer as a file-object as well. Not sure if that's
done though.

> 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.
> I wonder if real mainloops are done in this way and how bad/good is
> this implementation compared to a serious one. Any suggestion/hint/
> advice
> is well appreciated. Thanks,

It's ok, but of course more wasteful than it needs to be - better would be
full delegation to the OS.

Diez



More information about the Python-list mailing list