<p dir="ltr"><br>
On 27 Apr 2015 07:50, "Mark Shannon" <<a href="mailto:mark@hotpy.org">mark@hotpy.org</a>> wrote:<br>
> On 26/04/15 21:40, Yury Selivanov wrote:<br>
>><br>
>> But it's hard.  Iterating through something asynchronously?  Write a<br>
>> 'while True' loop.  Instead of 1 line you now have 5 or 6.  Want to<br>
>> commit your database transaction?  Instead of 'async with' you will<br>
>> write 'try..except..finally' block, with a very high probability to<br>
>> introduce a bug, because you don't rollback or commit properly or<br>
>> propagate exception.<br>
><br>
> I don't see why you can't do transactions using a 'with' statement.</p>
<p dir="ltr">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.</p>
<p dir="ltr">We knew about these problems going into PEP 3156 (<a href="http://python-notes.curiousefficiency.org/en/latest/pep_ideas/async_programming.html#using-special-methods-in-explicitly-asynchronous-code">http://python-notes.curiousefficiency.org/en/latest/pep_ideas/async_programming.html#using-special-methods-in-explicitly-asynchronous-code</a>) 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.</p>
<p dir="ltr">Cheers,<br>
Nick.<br>
</p>