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

« back to all changes in this revision

Viewing changes to examples/web/backend.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 backend request handler process.
 
2
 
 
3
This file uses zmq.web to implement the backend logic for load balanced
 
4
Tornado request handlers.
 
5
 
 
6
To run this example:
 
7
 
 
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.
 
13
 
 
14
"""
 
15
 
 
16
#-----------------------------------------------------------------------------
 
17
#  Copyright (c) 2012 Brian Granger, Min Ragan-Kelley
 
18
#
 
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
#-----------------------------------------------------------------------------
 
22
 
 
23
import logging
 
24
logging.basicConfig(level=logging.DEBUG)
 
25
import time
 
26
 
 
27
from zmq.eventloop import ioloop
 
28
ioloop.install()
 
29
from tornado import web
 
30
 
 
31
from zmq.web import ZMQApplication
 
32
 
 
33
class FooHandler(web.RequestHandler):
 
34
 
 
35
    def get(self):
 
36
        self.set_header('Handler', 'FooHandler')
 
37
        self.finish('bar')
 
38
 
 
39
class SleepHandler(web.RequestHandler):
 
40
 
 
41
    def get(self):
 
42
        t = float(self.get_argument('t',1.0))
 
43
        time.sleep(t)
 
44
        self.finish({'status':'awake','t':t})
 
45
 
 
46
application = ZMQApplication(
 
47
    [
 
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)
 
53
    ],
 
54
)
 
55
 
 
56
application.connect('tcp://127.0.0.1:5555')
 
57
ioloop.IOLoop.instance().start()