tornado icon indicating copy to clipboard operation
tornado copied to clipboard

Queue.get(timeout) waits indefinitly for timedelta of zero seconds

Open arnaudsjs opened this issue 2 years ago • 1 comments

The code snippet mentioned below will wait indefinitely until an element becomes available in the queue, while according to the documentation it should immediately raise a TimeoutError when the queue is empty.

from datetime import timedelta
from tornado import queues
q = queues.Queue()
q.get(timeout=timedelta(seconds=0))

This issue happens because timedelta(seconds=0) evaluates to a False value in a conditional expression. As such, the queues._set_timeout() method doesn't set the timeout when timeout=timedelta(seconds=0).

arnaudsjs avatar May 16 '23 09:05 arnaudsjs

Yes, that if timeout should be if timeout is not None (the same bug occurs with a float value of zero).

We also have a get_nowait() method which is a little more efficient, although it's not a drop-in replacement if you have to mix zero and non-zero timeouts (get_nowait() is a normal function and not a coroutine, so you have to call it without await).

bdarnell avatar May 16 '23 23:05 bdarnell