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:
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! _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/6CNWND... 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
On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro <gjcarneiro@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
On Mon, 20 Sept 2021 at 13:15, Chris Angelico <rosuav@gmail.com> wrote:
On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro <gjcarneiro@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-leave@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/CBBP7K... 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
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, <gjcarneiro@gmail.com> wrote:
On Mon, 20 Sept 2021 at 13:15, Chris Angelico <rosuav@gmail.com> wrote:
On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro <gjcarneiro@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-leave@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/CBBP7K... 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-leave@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/EIOECL... Code of Conduct: http://python.org/psf/codeofconduct/
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