Problem with coroutines old-style / new-style usage and features

Yahya Abou 'Imran yahya-abou-imran at protonmail.com
Thu Feb 1 15:19:46 EST 2018


>> @asyncio.coroutine
>> def recorder():
>> dialog = []
>> while True:
>> sent = yield dialog
>> if sent is not None:
>> name, things = sent
>> dialog.append(f'{name} says : {things}')
>This is not an asyncio coroutine. This is just a normal generator that
> you're sending to. So you should probably remove the decorator to
> prevent confusion. For that matter I'm not really sure why this isn't
> just a class with synchronous "add" and "get" methods.

Yeah it's true. I would just instantiate it, send to it and ask for a result. To pass it to next to start it and to retrieve a value is not really explicit.

[...]
>>It works well, but I was wondering if I could turn recorder into a new style coroutine...
>Why? It doesn't do anything asynchronously.

Hey, you're right... I was just, you know... "Async is so cool, let's just async everything!"

>If it were a class, then you could make the individual methods be
> coroutines if desired and await those.

Thanks for your advise Ian!



More information about the Python-list mailing list