1#!/usr/bin/python
2# gevent-test-requires-resource: webpy
3"""A web.py application powered by gevent"""
4
5from __future__ import print_function
6from gevent import monkey; monkey.patch_all()
7from gevent.pywsgi import WSGIServer
8import time
9import web # pylint:disable=import-error
10
11urls = ("/", "index",
12 '/long', 'long_polling')
13
14
15class index(object):
16 def GET(self):
17 return '<html>Hello, world!<br><a href="/long">/long</a></html>'
18
19
20class long_polling(object):
21 # Since gevent's WSGIServer executes each incoming connection in a separate greenlet
22 # long running requests such as this one don't block one another;
23 # and thanks to "monkey.patch_all()" statement at the top, thread-local storage used by web.ctx
24 # becomes greenlet-local storage thus making requests isolated as they should be.
25 def GET(self):
26 print('GET /long')
27 time.sleep(10) # possible to block the request indefinitely, without harming others
28 return 'Hello, 10 seconds later'
29
30
31if __name__ == "__main__":
32 application = web.application(urls, globals()).wsgifunc()
33 print('Serving on 8088...')
34 WSGIServer(('', 8088), application).serve_forever()
Next page: Example wsgiserver.py