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, <gjcarne...@gmail.com> wrote: > On Mon, 20 Sept 2021 at 13:15, Chris Angelico <ros...@gmail.com> wrote: > >> 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. >> > > 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 >> _______________________________________________ >> 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/ >> > > > -- > Gustavo J. A. M. Carneiro > Gambit Research > "The universe is always one step beyond logic." -- Frank Herbert > _______________________________________________ > 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/EIOECL4WGOA37SEXLBXYECZRQFIT7NQ7/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ 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/S2EX6GPFE6OI62DCENGRLUC7I4QW3FAV/ Code of Conduct: http://python.org/psf/codeofconduct/