[Python-ideas] asyncore: included batteries don't fit
Guido van Rossum
guido at python.org
Mon Oct 8 17:37:28 CEST 2012
On Mon, Oct 8, 2012 at 6:34 AM, Joachim König <him at online.de> wrote:
> 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
Ben just posted an example of how to do exactly that for getaddrinfo().
> 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.
However it must be done this seems a useful thing to solve once and
for all in a standard reactor specification and stdlib implementation.
(Ditto for signal handlers BTW.)
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list