This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author yselivanov
Recipients aeros, asvetlov, yselivanov
Date 2020-11-18.19:20:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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.
Date User Action Args
2020-11-18 19:20:35yselivanovsetrecipients: + yselivanov, asvetlov, aeros
2020-11-18 19:20:35yselivanovsetmessageid: <>
2020-11-18 19:20:35yselivanovlinkissue42392 messages
2020-11-18 19:20:34yselivanovcreate