[Python-ideas] Letting context managers react to yields inside their scope

Nathaniel Smith njs at pobox.com
Thu Apr 30 08:31:45 CEST 2015


On Wed, Apr 29, 2015 at 2:29 PM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> Nathanial,
>
> On 2015-04-29 4:22 PM, Nathaniel Smith wrote:
>>
>> Interaction with PEP 492
>> ========================
>>
>> PEP 492 currently proposes an ``async with`` statement.  Despite the
>> name, this is actually orthogonal to the proposal here: this PEP
>> proposes to allow context managers to react to suspensions inside
>> their scope, while ``async with`` makes context manager ``__enter__``
>> and ``__exit__`` methods into potential ``yield`` points.  If both
>> PEPs are accepted then we will presumably also want to add some kind
>> of ``__asuspend__`` and ``__aresume__`` methods to PEP 492-style
>> asynchronous context managers.
>
> I doubt that we'll need __asuspend__.  There shouldn't be
> a need to perform any blocking operations there.
>
> __suspend__ and __resume__ would be enough.

I couldn't think of any cases either, but said
__asuspend__/__aresume__ to play it safe in case I missed any :-). I
guess it makes sense, though, that from outside our thread,
suspend/resume operations are invisible -- no-one cares whether the
coroutine they're talking to is currently scheduled or currently
suspended, that's purely internal. Therefore, suspend/resume callbacks
should never need to block, yeah.

-n

-- 
Nathaniel J. Smith -- http://vorpus.org


More information about the Python-ideas mailing list