### Navigation
- [index](# "General Index")
- [modules](# "Python Module Index") |
- [next](# "tornado.process — Utilities for multiple processes") |
- [previous](# "tornado.locks – Synchronization primitives") |
- [Tornado 4.4.dev1 documentation](#) »
- [Coroutines and concurrency](#) »
# `tornado.queues` – Queues for coroutines
New in version 4.2.
### Classes
### Queue
*class *`tornado.queues.``Queue`(*maxsize=0*)[[source]](#)
Coordinate producer and consumer coroutines.
If maxsize is 0 (the default) the queue size is unbounded.
~~~
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.queues import Queue
q = Queue(maxsize=2)
@gen.coroutine
def consumer():
while True:
item = yield q.get()
try:
print('Doing work on %s' % item)
yield gen.sleep(0.01)
finally:
q.task_done()
@gen.coroutine
def producer():
for item in range(5):
yield q.put(item)
print('Put %s' % item)
@gen.coroutine
def main():
# Start consumer without waiting (since it never finishes).
IOLoop.current().spawn_callback(consumer)
yield producer() # Wait for producer to put all tasks.
yield q.join() # Wait for consumer to finish all tasks.
print('Done')
IOLoop.current().run_sync(main)
~~~
~~~
Put 0
Put 1
Doing work on 0
Put 2
Doing work on 1
Put 3
Doing work on 2
Put 4
Doing work on 3
Doing work on 4
Done
~~~
In Python 3.5, [`Queue`](# "tornado.queues.Queue") implements the async iterator protocol, so`consumer()` could be rewritten as:
~~~
async def consumer():
async for item in q:
try:
print('Doing work on %s' % item)
yield gen.sleep(0.01)
finally:
q.task_done()
~~~
Changed in version 4.3: Added `async for` support in Python 3.5.
`maxsize`
Number of items allowed in the queue.
`qsize`()[[source]](#)
Number of items in the queue.
`put`(*item*, *timeout=None*)[[source]](#)
Put an item into the queue, perhaps waiting until there is room.
Returns a Future, which raises [`tornado.gen.TimeoutError`](# "tornado.gen.TimeoutError") after atimeout.
`put_nowait`(*item*)[[source]](#)
Put an item into the queue without blocking.
If no free slot is immediately available, raise [`QueueFull`](# "tornado.queues.QueueFull").
`get`(*timeout=None*)[[source]](#)
Remove and return an item from the queue.
Returns a Future which resolves once an item is available, or raises[`tornado.gen.TimeoutError`](# "tornado.gen.TimeoutError") after a timeout.
`get_nowait`()[[source]](#)
Remove and return an item from the queue without blocking.
Return an item if one is immediately available, else raise[`QueueEmpty`](# "tornado.queues.QueueEmpty").
`task_done`()[[source]](#)
Indicate that a formerly enqueued task is complete.
Used by queue consumers. For each [`get`](# "tornado.queues.Queue.get") used to fetch a task, asubsequent call to [`task_done`](# "tornado.queues.Queue.task_done") tells the queue that the processingon the task is complete.
If a [`join`](# "tornado.queues.Queue.join") is blocking, it resumes when all items have beenprocessed; that is, when every [`put`](# "tornado.queues.Queue.put") is matched by a [`task_done`](# "tornado.queues.Queue.task_done").
Raises [`ValueError`](https://docs.python.org/3.4/library/exceptions.html#ValueError "(in Python v3.4)") [https://docs.python.org/3.4/library/exceptions.html#ValueError] if called more times than [`put`](# "tornado.queues.Queue.put").
`join`(*timeout=None*)[[source]](#)
Block until all items in the queue are processed.
Returns a Future, which raises [`tornado.gen.TimeoutError`](# "tornado.gen.TimeoutError") after atimeout.
### PriorityQueue
*class *`tornado.queues.``PriorityQueue`(*maxsize=0*)[[source]](#)
A [`Queue`](# "tornado.queues.Queue") that retrieves entries in priority order, lowest first.
Entries are typically tuples like `(priority number, data)`.
~~~
from tornado.queues import PriorityQueue
q = PriorityQueue()
q.put((1, 'medium-priority item'))
q.put((0, 'high-priority item'))
q.put((10, 'low-priority item'))
print(q.get_nowait())
print(q.get_nowait())
print(q.get_nowait())
~~~
~~~
(0, 'high-priority item')
(1, 'medium-priority item')
(10, 'low-priority item')
~~~
### LifoQueue
*class *`tornado.queues.``LifoQueue`(*maxsize=0*)[[source]](#)
A [`Queue`](# "tornado.queues.Queue") that retrieves the most recently put items first.
~~~
from tornado.queues import LifoQueue
q = LifoQueue()
q.put(3)
q.put(2)
q.put(1)
print(q.get_nowait())
print(q.get_nowait())
print(q.get_nowait())
~~~
~~~
1
2
3
~~~
### Exceptions
### QueueEmpty
*exception *`tornado.queues.``QueueEmpty`[[source]](#)
Raised by [`Queue.get_nowait`](# "tornado.queues.Queue.get_nowait") when the queue has no items.
### QueueFull
*exception *`tornado.queues.``QueueFull`[[source]](#)
Raised by [`Queue.put_nowait`](# "tornado.queues.Queue.put_nowait") when a queue is at its maximum size.
© Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.
- User's guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example - a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- Web framework
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- HTTP servers and clients
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- Asynchronous networking
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream-based TCP server
- Coroutines and concurrency
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- Integration with other services
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- Utilities
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- Frequently Asked Questions
- Release notes
- What's new in Tornado 4.3
- What's new in Tornado 4.2.1
- What's new in Tornado 4.2
- What's new in Tornado 4.1
- What's new in Tornado 4.0.2
- What's new in Tornado 4.0.1
- What's new in Tornado 4.0
- What's new in Tornado 3.2.2
- What's new in Tornado 3.2.1
- What's new in Tornado 3.2
- What's new in Tornado 3.1.1
- What's new in Tornado 3.1
- What's new in Tornado 3.0.2
- What's new in Tornado 3.0.1
- What's new in Tornado 3.0
- What's new in Tornado 2.4.1
- What's new in Tornado 2.4
- What's new in Tornado 2.3
- What's new in Tornado 2.2.1
- What's new in Tornado 2.2
- What's new in Tornado 2.1.1
- What's new in Tornado 2.1
- What's new in Tornado 2.0
- What's new in Tornado 1.2.1
- What's new in Tornado 1.2
- What's new in Tornado 1.1.1
- What's new in Tornado 1.1
- What's new in Tornado 1.0.1
- What's new in Tornado 1.0