1
"""A frontend web server with load balanced request handlers.
3
This example shows how to use zmq.web to run a Tornado web server with
4
individual request handlers in separate backend processes. This file implements
5
the frontend logic and needs to be run with 1 or more instances of
8
This version uses a streaming message protocol to enable the backend to send
9
the HTTP body back to the frontend/browser in multiple asynchronous chunks.
10
To enable streaming mode, you have to use ZMQStreamingApplicationProxy in
11
the frontend and ZMQStreamingHTTPRequest in the backend.
15
* Start one instance of frontend_stream.py.
16
* Start one or more instances of backend_stream.py.
17
* Hit the URLs http://127.0.0.1/foo and http://127.0.0.1/foo/sleep?t=1. The
18
t parameter of this last URL can be changed to something greater than 10 to
19
observe the timeout behavior.
26
#-----------------------------------------------------------------------------
27
# Copyright (c) 2012 Brian Granger, Min Ragan-Kelley
29
# Distributed under the terms of the New BSD License. The full license is in
30
# the file COPYING.BSD, distributed as part of this software.
31
#-----------------------------------------------------------------------------
34
logging.basicConfig(level=logging.DEBUG)
36
from zmq.eventloop import ioloop
38
from tornado import web
41
ZMQStreamingApplicationProxy, ZMQRequestHandlerProxy
44
proxy = ZMQStreamingApplicationProxy()
45
proxy.bind('tcp://127.0.0.1:5555')
47
application = web.Application(
48
# We use a timeout of 2000ms, after which a status of 504 is returned.
49
# All URLs beginning with /foo will be handled by the backend.
50
[(r"/foo\S*", ZMQRequestHandlerProxy, {'proxy':proxy,'timeout':10000})]
53
logging.info("Starting frontend HTTP server")
54
application.listen(8888)
55
ioloop.IOLoop.instance().start()