Add timeout parameter to Synchronization Primitives in asyncio

It would be very nice if the [Synchronization Primitives](https://docs.python.org/3/library/asyncio-sync.html) had a timeout parameter just like the [analogous classes](https://docs.python.org/3/library/threading.html#condition-objects) do in the threading module. Thank you for your consideration!

Note that you can wrap any of those methods with an asyncio.wait_for(). try: try: await asyncio.wait_for(lock.acquire(), 1.0) except asyncio.TimeoutError: # times out after 1 second print("deadlock!") return do_things_with_lock() finally: lock.release() Although I must admit, it would be convenient to have a timeout parameter directly in the methods like acquire, especially because it would make timeouts usable directly in the async context manager: async with lock.acquire(timeout=1.0): do_things_with_lock() On Mon, 20 Sept 2021 at 07:21, Andres Torres <andres.torreshalo@gmail.com> wrote:
-- Gustavo J. A. M. Carneiro Gambit Research "The universe is always one step beyond logic." -- Frank Herbert

On Mon, 20 Sept 2021 at 13:15, Chris Angelico <rosuav@gmail.com> wrote:
Right. If you needed to handle the timeout, you would need a try except. Else the TimeoutError exception that gets raised is unhandled and bubbles up. try: async with lock.acquire(timeout=1.0): do_things_with_lock() except asyncio.TimeoutError: print("deadlock!") return I mean, this is just slightly more convenient, probably slightly less scary for some people, but that's all. ChrisA
-- Gustavo J. A. M. Carneiro Gambit Research "The universe is always one step beyond logic." -- Frank Herbert

anyio provides a nice context manager that works in both asyncio and trio: ``` import anyio async def async_fn(): with anyio.move_on_after(1.0) as scope: async with lock: scope.deadline = math.inf await do_things_with_lock() ```

Note that you can wrap any of those methods with an asyncio.wait_for(). try: try: await asyncio.wait_for(lock.acquire(), 1.0) except asyncio.TimeoutError: # times out after 1 second print("deadlock!") return do_things_with_lock() finally: lock.release() Although I must admit, it would be convenient to have a timeout parameter directly in the methods like acquire, especially because it would make timeouts usable directly in the async context manager: async with lock.acquire(timeout=1.0): do_things_with_lock() On Mon, 20 Sept 2021 at 07:21, Andres Torres <andres.torreshalo@gmail.com> wrote:
-- Gustavo J. A. M. Carneiro Gambit Research "The universe is always one step beyond logic." -- Frank Herbert

On Mon, 20 Sept 2021 at 13:15, Chris Angelico <rosuav@gmail.com> wrote:
Right. If you needed to handle the timeout, you would need a try except. Else the TimeoutError exception that gets raised is unhandled and bubbles up. try: async with lock.acquire(timeout=1.0): do_things_with_lock() except asyncio.TimeoutError: print("deadlock!") return I mean, this is just slightly more convenient, probably slightly less scary for some people, but that's all. ChrisA
-- Gustavo J. A. M. Carneiro Gambit Research "The universe is always one step beyond logic." -- Frank Herbert

anyio provides a nice context manager that works in both asyncio and trio: ``` import anyio async def async_fn(): with anyio.move_on_after(1.0) as scope: async with lock: scope.deadline = math.inf await do_things_with_lock() ```
participants (4)
-
Andres Torres
-
Chris Angelico
-
Gustavo Carneiro
-
Thomas Grainger