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

Koos Zevenhoven koos.zevenhoven at aalto.fi
Tue May 5 17:49:47 CEST 2015


On 2015-05-05 17:57, Ludovic Gasc wrote:
>
> 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.
>

Thank you for your email!

I've been following quite a bit of the PEP492 discussions, but not sure 
if I have missed something. If there is something about await outside 
async def that goes further than "It is a SyntaxError to use await 
outside of an async def function (like it is a SyntaxError to use yield 
outside of def function.)", which is directly from the PEP, I've missed 
that. A link or pointer would be helpful.

In any case, I think I understand the problem you are referring to, but 
is that any different from forgetting a postfix "_s" in the approach you 
mention below?

> 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.

Surely that is possible, although may of course be hard to implement :). 
I think this is related to this earlier suggestion by Joshua Bartlett 
(which I do like):

https://mail.python.org/pipermail/python-ideas/2013-January/018519.html

However, I don't think it solves *this* problem. It would just become a 
more verbose version of what I suggested.

>
> 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 ;-)
>

In my mind, this is not at all about explicit vs. implicit. It is mostly 
about letting the coroutines know what kind of context they are being 
run from. Anyway, I'm pretty sure there are plenty of people in the 
Python community who don't think efficiency is enough, but that is a 
matter of personal preference. I want everything, and that's why I'm 
using Python ;).

-- Koos

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150505/8990546d/attachment.html>


More information about the Python-ideas mailing list