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

« back to all changes in this revision

Viewing changes to examples/web/frontend_stream.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
"""A frontend web server with load balanced request handlers.
 
2
 
 
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
 
6
backend_stream.py.
 
7
 
 
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.
 
12
 
 
13
To run this example:
 
14
 
 
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.
 
20
 
 
21
Authors:
 
22
 
 
23
* Brian Granger
 
24
"""
 
25
 
 
26
#-----------------------------------------------------------------------------
 
27
#  Copyright (c) 2012 Brian Granger, Min Ragan-Kelley
 
28
#
 
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
#-----------------------------------------------------------------------------
 
32
 
 
33
import logging
 
34
logging.basicConfig(level=logging.DEBUG)
 
35
 
 
36
from zmq.eventloop import ioloop
 
37
ioloop.install()
 
38
from tornado import web
 
39
 
 
40
from zmq.web import (
 
41
    ZMQStreamingApplicationProxy, ZMQRequestHandlerProxy
 
42
)
 
43
 
 
44
proxy = ZMQStreamingApplicationProxy()
 
45
proxy.bind('tcp://127.0.0.1:5555')
 
46
 
 
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})]
 
51
)
 
52
 
 
53
logging.info("Starting frontend HTTP server")
 
54
application.listen(8888)
 
55
ioloop.IOLoop.instance().start()