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
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
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()
```
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!")
>
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
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