how do you implement a reactor without a select?

Alex Martelli aleax at mac.com
Mon May 7 10:39:24 EDT 2007


Michele Simionato <michele.simionato at gmail.com> wrote:

> 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,

Module sched in Python's standard library may suggest one clearly-better
approach: when you know in advance when future events are scheduled for,
sleep accordingly (rather than polling every millisecond).  sched's
sources are simple enough to study, and its architecture clean and
strong enough that it's easy to extend to other cases, e.g. where
previously-unscheduled events may be delivered from other threads,
without necessarily hacking the sources.

Specifically, sched implements the Dependency Injection DP: rather than
just calling time.time and time.sleep, it accepts those two callables
upon initialization.  This makes it easy, among many other
customizations, to pass instead of time.sleep a user-coded callable
(typically a bound method) that "sleeps" by a wait-with-timeout on a
Queue (so that other threads, by putting an event on the Queue in
question, immediately wake up the scheduler, etc, etc).


Alex



More information about the Python-list mailing list