]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Move doctests from queues.rst to queues.py. 1409/head
authorA. Jesse Jiryu Davis <jesse@mongodb.com>
Fri, 10 Apr 2015 03:58:17 +0000 (23:58 -0400)
committerA. Jesse Jiryu Davis <jesse@mongodb.com>
Fri, 10 Apr 2015 03:58:17 +0000 (23:58 -0400)
docs/queues.rst
tornado/queues.py

index 806d05c649368a8ed9a29aa8083d6d215d3b527a..5c8716eb0187ce010ca88f572c9328a02aed5229 100644 (file)
    .. autoclass:: Queue
     :members:
 
-    .. testcode::
-
-        q = queues.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():
-            consumer()           # Start consumer.
-            yield producer()     # Wait for producer to put all tasks.
-            yield q.join()       # Wait for consumer to finish all tasks.
-            print('Done')
-
-        io_loop.run_sync(main)
-
-    .. testoutput::
-
-        Put 0
-        Put 1
-        Put 2
-        Doing work on 0
-        Doing work on 1
-        Put 3
-        Doing work on 2
-        Put 4
-        Doing work on 3
-        Doing work on 4
-        Done
-
-
    PriorityQueue
    ^^^^^^^^^^^^^
    .. autoclass:: PriorityQueue
     :members:
 
-    .. testcode::
-
-        q = queues.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())
-
-    .. testoutput::
-
-        (0, 'high-priority item')
-        (1, 'medium-priority item')
-        (10, 'low-priority item')
-
    LifoQueue
    ^^^^^^^^^
    .. autoclass:: LifoQueue
     :members:
 
-    .. testcode::
-
-        q = queues.LifoQueue()
-        q.put(3)
-        q.put(2)
-        q.put(1)
-
-        print(q.get_nowait())
-        print(q.get_nowait())
-        print(q.get_nowait())
-
-    .. testoutput::
-
-        1
-        2
-        3
-
    Exceptions
    ----------
 
index 128c415ce1c6eb3f6da16719c6dc690bc5b7e1dd..55ab4834ed244d722305f4e6d4b84c7629e7da6d 100644 (file)
@@ -48,6 +48,49 @@ class Queue(object):
     """Coordinate producer and consumer coroutines.
 
     If maxsize is 0 (the default) the queue size is unbounded.
+
+    .. testcode::
+
+        q = queues.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():
+            consumer()           # Start consumer.
+            yield producer()     # Wait for producer to put all tasks.
+            yield q.join()       # Wait for consumer to finish all tasks.
+            print('Done')
+
+        io_loop.run_sync(main)
+
+    .. testoutput::
+
+        Put 0
+        Put 1
+        Put 2
+        Doing work on 0
+        Doing work on 1
+        Put 3
+        Doing work on 2
+        Put 4
+        Doing work on 3
+        Doing work on 4
+        Done
     """
     def __init__(self, maxsize=0):
         if maxsize is None:
@@ -220,6 +263,23 @@ class PriorityQueue(Queue):
     """A `.Queue` that retrieves entries in priority order, lowest first.
 
     Entries are typically tuples like ``(priority number, data)``.
+
+    .. testcode::
+
+        q = queues.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())
+
+    .. testoutput::
+
+        (0, 'high-priority item')
+        (1, 'medium-priority item')
+        (10, 'low-priority item')
     """
     def _init(self):
         self._queue = []
@@ -232,7 +292,25 @@ class PriorityQueue(Queue):
 
 
 class LifoQueue(Queue):
-    """A `.Queue` that retrieves the most recently put items first."""
+    """A `.Queue` that retrieves the most recently put items first.
+
+    .. testcode::
+
+        q = queues.LifoQueue()
+        q.put(3)
+        q.put(2)
+        q.put(1)
+
+        print(q.get_nowait())
+        print(q.get_nowait())
+        print(q.get_nowait())
+
+    .. testoutput::
+
+        1
+        2
+        3
+    """
     def _init(self):
         self._queue = []