On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro <gjcarne...@gmail.com> 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!")
>        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()
>

How would this signal a timeout to your code? It looks lovely and
clean, but there still need to be two branches, so you're not really
going to avoid the try/except layer.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CBBP7KHS27AF63EW72VXEYZVFLRBSY4R/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to