[Python-ideas] asyncore: included batteries don't fit

Joachim König him at online.de
Mon Oct 8 15:34:52 CEST 2012


On 08/10/2012 03:41 Ben Darnell wrote:
> As for the higher-level question of what asynchronous code should look
> like, there's a lot more room for spirited debate, and I don't think
> there's enough consensus to declare a One True Way.  Personally, I'm
> -1 on greenlets as a general solution (what if you have to call
> MySQLdb or getaddrinfo?)

The caller of such a potentially blocking function could:

* spawn a new thread for the call
* call the function inside the thread and collect return value or exception
* register the thread (id) to inform the event loop (scheduler) it's 
waiting for it's completion
* yield (aka "switch" in greenlet) to the event loop / scheduler
* upon continuation either continue with the result or reraise the 
exception that happened in the thread

Unfortunately on Unix systems select/poll/kqueue cannot specify threads as
event resources, so an additional pipe descriptor would be needed for 
the scheduler
to detect thread completions without blocking (threads would write to 
the pipe upon
completion), not elegant but doable.

Joachim





More information about the Python-ideas mailing list