[Python-Dev] PEP 525, third round, better finalization

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Sep 3 21:22:47 EDT 2016


Nick Coghlan wrote:
> For synchronous code, that's a relatively easy burden to push back
> onto the programmer - assuming fair thread scheduling, a with
> statement can ensure reliably ensure prompt resource cleanup.
> 
> That assurance goes out the window as soon as you explicitly pause
> code execution inside the body of the with statement - it doesn't
> matter whether its via yield, yield from, or await, you've completely
> lost that assurance of immediacy.

I don't see how this is any worse than a thread containing
an ordinary with-statement that waits for something that
will never happen. If that's the case, then you've got a
deadlock, and you have more to worry about than resources
not being released.

I think what all this means is that an event loop must
not simply drop async tasks on the floor. If it's asked
to cancel a task, it should do that by throwing an
appropriate exception into it and letting it unwind
itself.

To go along with that, the programmer needs to understand
that he can't just fire off a task and abandon it if it
uses external resources and is not guaranteed to finish
under its own steam. He needs to arrange a timeout or
other mechanism to cancel it if it doesn't complete in a
timely manner.

If those things are done, an async with should be exactly
as adequate for resource cleanup as an ordinary with is in
a thread. It also shouldn't be necessary to have any
special protocol for finalising an async generator; async
with together with a way of throwing an exception into a
task should be all that's needed.

-- 
Greg



More information about the Python-Dev mailing list