[Python-Dev] PEP 492: No new syntax is required

Nick Coghlan ncoghlan at gmail.com
Mon Apr 27 00:24:20 CEST 2015


On 27 Apr 2015 07:50, "Mark Shannon" <mark at hotpy.org> wrote:
> On 26/04/15 21:40, Yury Selivanov wrote:
>>
>> But it's hard.  Iterating through something asynchronously?  Write a
>> 'while True' loop.  Instead of 1 line you now have 5 or 6.  Want to
>> commit your database transaction?  Instead of 'async with' you will
>> write 'try..except..finally' block, with a very high probability to
>> introduce a bug, because you don't rollback or commit properly or
>> propagate exception.
>
> I don't see why you can't do transactions using a 'with' statement.

Because you need to pass control back to the event loop from the *__exit__*
method in order to wait for the commit/rollback operation without blocking
the scheduler. The "with (yield from cm())" formulation doesn't allow
either __enter__ *or* __exit__ to suspend the coroutine to wait for IO, so
you have to do the IO up front and return a fully synchronous (but still
non-blocking) CM as the result.

We knew about these problems going into PEP 3156 (
http://python-notes.curiousefficiency.org/en/latest/pep_ideas/async_programming.html#using-special-methods-in-explicitly-asynchronous-code)
so it's mainly a matter of having enough experience with asyncio now to be
able to suggest specific syntactic sugar to make the right way and the easy
way the same way.

Cheers,
Nick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150427/7c15b691/attachment-0001.html>


More information about the Python-Dev mailing list