On Fri, Dec 27, 2013 at 9:35 AM, Antoine Pitrou <[email protected]> wrote:
> On Tue, 24 Dec 2013 01:12:57 +0100
> Tobias Oberstein
> <[email protected]> wrote:
>>
>> So you say I call the user-level functions (which may be coroutines)
>> like this from within my protocol implementation?
>>
>> res = self.onMessage(message)
>> if PY3 and (isinstance(res, asyncio.futures.Future) or
>>              inspect.isgenerator(res)):
>>     asyncio.async(res)
>>
>> => onMessage is the user code
>
> Yes. Of course, ideally there should be a terser way of saying
> "(isinstance(res, asyncio.futures.Future) or inspect.isgenerator(res))"
> :-)

You shouldn't use inspect.isgenerator() though -- you should use
asyncio.iscoroutine(). (It's currently undocumented but we should fix
that.)

The reason there's no more compact way to express this is probably
that in most cases you shouldn't dynamically have to know this.
Usually you already know that something is waitable, and then you just
use "yield from" on it without the test, or you need a Future anyway
(e.g. to add a callback) and then you just use asyncio.async().

>> Also: will asyncio.async schedule via reentering the event loop or how?
>
> Off the top of my head, no. But better confirm by reading the source
> code :-)

Not sure what the question means. But when you call async() you are
still responsible for yielding to the event loop -- there is no second
event loop that runs background tasks for you in a separate thread.
:-)

(Although an interesting area of research might be to figure out if we
can invent a mixture of threads and event loops to emulate Go's
goroutines, which are such a mixture supported by special syntax in
the compiler.)

-- 
--Guido van Rossum (python.org/~guido)

Reply via email to