how do you implement a reactor without a select?

Alex Martelli aleax at mac.com
Tue May 8 01:18:39 EDT 2007


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

> On May 8, 4:53 am, a... at mac.com (Alex Martelli) wrote:
> > What do you expect from "timers on Linux" that you could not get with a
> > simple "sleep for the next N milliseconds"?  A timer (on Linux or
> > elsewhere) can jog your process N milliseconds from now, e.g. with a
> > SIGALRM or SIGPROF, and you can set one with the setitimer syscall
> > (presumably accessible via ctypes, worst case -- I've never used it from
> > Python, yet), but how would that help you (compared to plain sleep,
> > select, poll, or whatever else best fits your need)?
> 
> I hoped there was a library such thay I could register a Python
> callable (say
> a thunk) and having it called by the linux timer at time t without
> blocking
> my process. But if a Linux timer will just send to my process an
> alarm, I would need to code myself a mechanism waiting for the alarm
> and doing the function call. In that case as you say, I would be
> better off with a select+timeout or a even with a queue+timeout, which
> already do most of the job.

Python or not, I don't know of a way to "register a callback" from the
OS without using threads.  Considering that your callback WOULD be
executing on a different thread no matter what (your "only" thread being
blocked on some blocking syscall, or executing other code -- having
other code in your process suddenly start executing at that point is
pre-emptive threading, by whatever name you choose to call it), it's not
clear to me why the "avoiding threads" issue should matter to you.


Alex



More information about the Python-list mailing list