Author yselivanov
Recipients aeros, asvetlov, yselivanov
Date 2020-11-18.19:20:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1605727235.09.0.614556228567.issue42392@roundup.psfhosted.org>
In-reply-to
Content
Yeah, I follow the reasoning.

My use case:

   class Something:
      def __init__(self):
          self._lock = asyncio.Lock()

      async def do_something():
          async with self._lock:
              ...

And `Something` won't be created in a coroutine. So now I have to jump through the hoops to implement lazy lock instantiation.


> But the lock is tightly coupled with a loop instance. In other words, the loop belongs to the loop.
>The lock cannot be used after the loop dies (stopped and closed).

I agree. Maybe the solution should be this then:


    class asyncio.Lock:

       def _get_loop(self):
           loop = asyncio.get_running_loop()
           if self._loop is None:
                self._loop = loop
           if loop is not self._loop: raise
           if not loop.is_running(): raise

       async def acquire(self):
           loop = self._get_loop()
           ...

This is what would guarantee all protections you want and would also allow to instantiate `asyncio.Lock` in class constructors, simplifying code.
History
Date User Action Args
2020-11-18 19:20:35yselivanovsetrecipients: + yselivanov, asvetlov, aeros
2020-11-18 19:20:35yselivanovsetmessageid: <1605727235.09.0.614556228567.issue42392@roundup.psfhosted.org>
2020-11-18 19:20:35yselivanovlinkissue42392 messages
2020-11-18 19:20:34yselivanovcreate