1
"""A backend request handler process.
3
This file uses zmq.web to implement the backend logic for load balanced
4
Tornado request handlers.
8
* Start one instance of frontend.py.
9
* Start one or more instances of backend.py.
10
* Hit the URLs http://127.0.0.1/foo and http://127.0.0.1/foo/sleep?t=1. The
11
t parameter of this last URL can be changed to something greater than 2 to
12
observe the timeout behavior.
16
#-----------------------------------------------------------------------------
17
# Copyright (c) 2012 Brian Granger, Min Ragan-Kelley
19
# Distributed under the terms of the New BSD License. The full license is in
20
# the file COPYING.BSD, distributed as part of this software.
21
#-----------------------------------------------------------------------------
24
logging.basicConfig(level=logging.DEBUG)
27
from zmq.eventloop import ioloop
29
from tornado import web
31
from zmq.web import ZMQApplication
33
class FooHandler(web.RequestHandler):
36
self.set_header('Handler', 'FooHandler')
39
class SleepHandler(web.RequestHandler):
42
t = float(self.get_argument('t',1.0))
44
self.finish({'status':'awake','t':t})
46
application = ZMQApplication(
48
# A single ZMQApplication can run multiple request handlers, but the
49
# frontend must use a URL regular expression that matches all of the
50
# patterns in the backend.
51
(r"/foo", FooHandler),
52
(r"/foo/sleep", SleepHandler)
56
application.connect('tcp://127.0.0.1:5555')
57
ioloop.IOLoop.instance().start()