summaryrefslogtreecommitdiff
path: root/examples/eventloop/web.py
diff options
context:
space:
mode:
authorMicah Anderson <micah@riseup.net>2014-11-11 11:53:55 -0500
committerMicah Anderson <micah@riseup.net>2014-11-11 11:53:55 -0500
commit7d5c3dcd969161322deed6c43f8a6a3cb92c3369 (patch)
tree109b05c88c7252d7609ef324d62ef9dd7f06123f /examples/eventloop/web.py
parent44be832c5708baadd146cb954befbc3dcad8d463 (diff)
upgrade to 14.4.1upstream/14.4.1
Diffstat (limited to 'examples/eventloop/web.py')
-rw-r--r--examples/eventloop/web.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/eventloop/web.py b/examples/eventloop/web.py
new file mode 100644
index 0000000..1285f95
--- /dev/null
+++ b/examples/eventloop/web.py
@@ -0,0 +1,46 @@
+import zmq
+from zmq.eventloop import ioloop, zmqstream
+
+"""
+ioloop.install() must be called prior to instantiating *any* tornado objects,
+and ideally before importing anything from tornado, just to be safe.
+
+install() sets the singleton instance of tornado.ioloop.IOLoop with zmq's
+IOLoop. If this is not done properly, multiple IOLoop instances may be
+created, which will have the effect of some subset of handlers never being
+called, because only one loop will be running.
+"""
+
+ioloop.install()
+
+import tornado
+import tornado.web
+
+
+"""
+this application can be used with echostream.py, start echostream.py,
+start web.py, then every time you hit http://localhost:8888/,
+echostream.py will print out 'hello'
+"""
+
+def printer(msg):
+ print (msg)
+
+ctx = zmq.Context()
+s = ctx.socket(zmq.REQ)
+s.connect('tcp://127.0.0.1:5555')
+stream = zmqstream.ZMQStream(s)
+stream.on_recv(printer)
+
+class TestHandler(tornado.web.RequestHandler):
+ def get(self):
+ print ("sending hello")
+ stream.send("hello")
+ self.write("hello")
+application = tornado.web.Application([(r"/", TestHandler)])
+
+if __name__ == "__main__":
+ application.listen(8888)
+ ioloop.IOLoop.instance().start()
+
+