Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
arnaudsjs opened this issue May 16, 2023 · 1 comment
Open
Labels

Comments

@arnaudsjs
Copy link

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).

@bdarnell
Copy link
Member

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants