~ubuntu-branches/ubuntu/wily/pyzmq/wily

« back to all changes in this revision

Viewing changes to examples/eventloop/web.py

  • Committer: Package Import Robot
  • Author(s): Julian Taylor
  • Date: 2013-02-24 19:23:15 UTC
  • mfrom: (1.2.1) (9 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20130224192315-qhmwp3m3ymk8r60d
Tags: 2.2.0.1-1
* New upstream release
* relicense debian packaging to LGPL-3
* update watch file to use github directly
  thanks to Bart Martens for the file
* add autopkgtests
* drop obsolete DM-Upload-Allowed
* bump standard to 3.9.4, no changes required

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import zmq
 
2
from zmq.eventloop import ioloop, zmqstream
 
3
 
 
4
"""
 
5
ioloop.install() must be called prior to instantiating *any* tornado objects,
 
6
and ideally before importing anything from tornado, just to be safe.
 
7
 
 
8
install() sets the singleton instance of tornado.ioloop.IOLoop with zmq's
 
9
IOLoop. If this is not done properly, multiple IOLoop instances may be
 
10
created, which will have the effect of some subset of handlers never being
 
11
called, because only one loop will be running.
 
12
"""
 
13
 
 
14
ioloop.install()
 
15
 
 
16
import tornado
 
17
import tornado.web
 
18
 
 
19
 
 
20
"""
 
21
this application can be used with echostream.py, start echostream.py,
 
22
start web.py, then every time you hit http://localhost:8888/,
 
23
echostream.py will print out 'hello'
 
24
"""
 
25
 
 
26
def printer(msg):
 
27
    print (msg)
 
28
 
 
29
ctx = zmq.Context()
 
30
s = ctx.socket(zmq.REQ)
 
31
s.connect('tcp://127.0.0.1:5555')
 
32
stream = zmqstream.ZMQStream(s)
 
33
stream.on_recv(printer)
 
34
 
 
35
class TestHandler(tornado.web.RequestHandler):
 
36
    def get(self):
 
37
        print ("sending hello")
 
38
        stream.send("hello")
 
39
        self.write("hello")
 
40
application = tornado.web.Application([(r"/", TestHandler)])
 
41
 
 
42
if __name__ == "__main__":
 
43
    application.listen(8888)
 
44
    ioloop.IOLoop.instance().start()
 
45
 
 
46
 
 
47