summaryrefslogtreecommitdiffstats
path: root/external_libs/python/pyzmq-14.7.0/examples/eventloop/asyncweb.py
blob: 06b03f3f240b0461c1c0de3ec145da485c6d21ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Async web request example with tornado.

Requests to localhost:8888 will be relayed via 0MQ to a slow responder,
who will take 1-5 seconds to respond.  The tornado app will remain responsive
duriung this time, and when the worker replies, the web request will finish.

A '.' is printed every 100ms to demonstrate that the zmq request is not blocking
the event loop.
"""


import sys
import random
import threading
import time

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
from tornado import web


def slow_responder():
    """thread for slowly responding to replies."""
    ctx = zmq.Context.instance()
    socket = ctx.socket(zmq.REP)
    socket.linger = 0
    socket.bind('tcp://127.0.0.1:5555')
    i=0
    while True:
        msg = socket.recv()
        print "\nworker received %r\n" % msg
        time.sleep(random.randint(1,5))
        socket.send(msg + " to you too, #%i" % i)
        i+=1

def dot():
    """callback for showing that IOLoop is still responsive while we wait"""
    sys.stdout.write('.')
    sys.stdout.flush()

def printer(msg):
    print (msg)

class TestHandler(web.RequestHandler):
    
    @web.asynchronous
    def get(self):
        ctx = zmq.Context.instance()
        s = ctx.socket(zmq.REQ)
        s.connect('tcp://127.0.0.1:5555')
        # send request to worker
        s.send('hello')
        loop = ioloop.IOLoop.instance()
        self.stream = zmqstream.ZMQStream(s)
        self.stream.on_recv(self.handle_reply)
    
    def handle_reply(self, msg):
        # finish web request with worker's reply
        reply = msg[0]
        print "\nfinishing with %r\n" % reply,
        self.stream.close()
        self.write(reply)
        self.finish()

def main():
    worker = threading.Thread(target=slow_responder)
    worker.daemon=True
    worker.start()
    
    application = web.Application([(r"/", TestHandler)])
    beat = ioloop.PeriodicCallback(dot, 100)
    beat.start()
    application.listen(8888)
    try:
        ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        print ' Interrupted'
    
    
if __name__ == "__main__":
    main()