[Python-ideas] async/await and synchronous code (and PEP492 ?)

Ludovic Gasc gmludo at gmail.com
Tue May 5 16:57:36 CEST 2015


Hi Koos,

2015-05-05 15:55 GMT+02:00 Koos Zevenhoven <koos.zevenhoven at aalto.fi>:

> With this addition to Python, one could write libraries that work both
> async and non-async. When await is not inside async def, one would expect
> it to potentially do blocking IO, just like an await inside async def would
> suggest that there is a yield/suspend somewhere in there.



To be honest with you, I'd this type of ideas back in my mind, but for now,
I've no suggestion to avoid end-developer nor low-developer nightmares.

For example, we may detect if it's async or not if you have: result = await
response.payload() or result = response.payload()
The issue I see with that and certainly already explained during PEP492
discussions, is that it will be difficult for the developer to spot where
he is forgotten await keyword, because he won't have errors.

Moreover, in the use cases where async is less efficient that sync, it
should be interesting to be possible, maybe with a context manager to
define a block of code where all await are in fact sync (without to use
event loop). But, even if a talentuous low-developper find a solution to
implement this idea, because I'm not sure it's technically possible, in
fact it will more easier even for end-developers to use the sync library
version of this need.

FYI, I've made an yocto library for my company where I need to be sync for
some use cases and async for some other use cases.
For the sync and async public API where the business logic behind most
functions are identical, I've followed the same pattern as in Python-LDAP:
http://www.python-ldap.org/doc/html/ldap.html#sending-ldap-requests
I've postfixed all sync functions by "_s".

For a more complex library, it may possible to have two differents classes
with explicit names.

At least to me, it's enough to work efficiently, explicit is better than
implicit ;-)
--
Ludovic Gasc (GMLudo)
http://www.gmludo.eu/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150505/9ba68c7d/attachment-0001.html>


More information about the Python-ideas mailing list