]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Make specific example code for Condition. 1396/head
authorA. Jesse Jiryu Davis <jesse@mongodb.com>
Thu, 26 Mar 2015 10:31:57 +0000 (06:31 -0400)
committerA. Jesse Jiryu Davis <jesse@mongodb.com>
Thu, 26 Mar 2015 10:31:57 +0000 (06:31 -0400)
docs/locks.rst

index 35441aa7f2a683670c6add186df0345e90df7851..41c1e2bde4090dc6c08d9faf3544dc623f1392e7 100644 (file)
@@ -11,65 +11,64 @@ place of those from the standard library--they are meant to coordinate Tornado
 coroutines in a single-threaded app, not to protect shared objects in a
 multithreaded app.)*
 
-.. _the-wait-notify-pattern:
+.. automodule:: tornado.locks
 
-The Wait / Notify Pattern
-=========================
-Tornado locks follow a "wait / notify pattern": one coroutine waits to be
-notified by another. Take `~tornado.locks.Condition` as an example:
+   Condition
+   ---------
+   .. autoclass:: Condition
+    :members:
 
-.. testcode::
+    With a `Condition`, coroutines can wait to be notified by other coroutines:
 
-    from tornado import ioloop, gen, locks
+    .. testcode::
 
+        from tornado import ioloop, gen, locks
 
-    io_loop = ioloop.IOLoop.current()
-    condition = locks.Condition()
 
+        io_loop = ioloop.IOLoop.current()
+        condition = locks.Condition()
 
-    @gen.coroutine
-    def waiter():
-        print("I'll wait right here")
-        yield condition.wait()  # Yield a Future.
-        print("I'm done waiting")
 
+        @gen.coroutine
+        def waiter():
+            print("I'll wait right here")
+            yield condition.wait()  # Yield a Future.
+            print("I'm done waiting")
 
-    @gen.coroutine
-    def notifier():
-        print("About to notify")
-        condition.notify()
-        print("Done notifying")
 
+        @gen.coroutine
+        def notifier():
+            print("About to notify")
+            condition.notify()
+            print("Done notifying")
 
-    @gen.coroutine
-    def runner():
-        # Yield two Futures; wait for waiter() and notifier() to finish.
-        yield [waiter(), notifier()]
 
-    io_loop.run_sync(runner)
+        @gen.coroutine
+        def runner():
+            # Yield two Futures; wait for waiter() and notifier() to finish.
+            yield [waiter(), notifier()]
 
-.. testoutput::
+        io_loop.run_sync(runner)
 
-    I'll wait right here
-    About to notify
-    Done notifying
-    I'm done waiting
+    .. testoutput::
 
-Wait-methods take an optional ``timeout`` argument, which is either an
-absolute timestamp::
+        I'll wait right here
+        About to notify
+        Done notifying
+        I'm done waiting
 
-    io_loop = ioloop.IOLoop.current()
+    `wait` takes an optional ``timeout`` argument, which is either an absolute
+    timestamp::
 
-    # Wait up to 1 second for a notification.
-    yield condition.wait(deadline=io_loop.time() + 1)
+        io_loop = ioloop.IOLoop.current()
 
-...or a `datetime.timedelta` for a deadline relative to the current time::
+        # Wait up to 1 second for a notification.
+        yield condition.wait(deadline=io_loop.time() + 1)
 
-    # Wait up to 1 second.
-    yield condition.wait(deadline=datetime.timedelta(seconds=1))
+    ...or a `datetime.timedelta` for a deadline relative to the current time::
 
-The method raises `tornado.gen.TimeoutError` if there's no notification
-before the deadline.
+        # Wait up to 1 second.
+        yield condition.wait(deadline=datetime.timedelta(seconds=1))
 
-.. automodule:: tornado.locks
-    :members:
+    The method raises `tornado.gen.TimeoutError` if there's no notification
+    before the deadline.