[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Thomas Grainger
I'd imagine that context manager here simply wouldn't cancel the current task if the lock can be acquired in time: ``` async with lock.acquire(timeout=1.0): await do_things_with_lock() ``` On Mon, 20 Sep 2021, 14:15 Gustavo Carneiro, wrote: > On Mon, 20 Sept 2021 at 13:15, Chris Ang

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Gustavo Carneiro
On Mon, 20 Sept 2021 at 13:15, Chris Angelico wrote: > On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro > wrote: > > > > 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.T

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Thomas Grainger
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() ```

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Chris Angelico
On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro wrote: > > 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!") >

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Gustavo Carneiro
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

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-19 Thread Blue
This is already supported with [`asyncio.wait_for()`]( https://docs.python.org/3/library/asyncio-task.html#asyncio.wait_for). For example: `await asyncio.wait_for(asyncio.Event().wait(), timeout=60)` ___ Python-ideas mailing list -- python-ideas@python.o