[Async-sig] Asynchronous cleanup is a problem

Cory Benfield cory at lukasa.co.uk
Wed Jul 6 09:42:27 EDT 2016


> On 6 Jul 2016, at 13:09, David Beazley <dave at dabeaz.com> wrote:
> 
> Curio uses asynchronous context managers for much more than closing sockets (which frankly is the least interesting thing).   For example, they're used extensively with synchronization primitives such as Locks, Semaphores, Events, Queues, and other such things.   The ability to use coroutines in the __aexit__() method is an essential part of these primitives because it allows task scheduling decisions to be made in conjunction with synchronization events such as lock releases.   For example, you can implement fair-locking or various forms of priority scheduling.   Curio also uses asynchronous context managers for timeouts and other related functionality where coroutines have to be used in __aexit__.  I would expect coroutines in __aexit__ to also be useful in more advanced contexts such as working with databases, dealing with transactions, and other kinds of processing where asynchronous I/O might be involved.

For my own edification, Dave, do you mind if I dive into this a little bit? My suspicion is that this problem is rather unique to curio (or, at least, does not so strongly effect event loop implementations), and I’d just like to get a handle on it. It’s important to me that we don’t blow away curio, so where it differs from event loops I’d like to understand what it’s doing.

In the case of an event loop implementation, all of the above can be implemented simply by scheduling a callback which can then do whatever it needs to do. For example, scheduling a fair lock can be implemented synchronously, either literally or by means of something like asyncio.call_soon(). The only advantage I can see for event loops in being able to use a coroutine here is that they can suspend execution of the outer block until such time as they *know* that the next task has been scheduled into the critical section, which while useful does not strike me as *necessary*.

However, as far as I know curio doesn’t have the ability to schedule an operation in a synchronous manner by means of something like a Future. Is that correct? If there is no way in curio to spawn a task to occur later without having to await on it, then clearly there is no option but to allow coroutines in __aexit__ and finally: how else could curio operate?

Cory
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.python.org/pipermail/async-sig/attachments/20160706/99c366a1/attachment.sig>


More information about the Async-sig mailing list