diff options
author | Adam R. Smith <a2smith@ucsd.edu> | 2011-04-07 09:49:04 -0700 |
---|---|---|
committer | Adam R. Smith <a2smith@ucsd.edu> | 2011-04-07 09:49:04 -0700 |
commit | a959341b73159008f39a5a9d3ee61abb741492c9 (patch) | |
tree | a6f73950a04cc5eebaaec6015374137f2d3a5630 /docs | |
parent | 307db32c022aba139d2a872e15d3b1dc8c23b6b0 (diff) | |
download | bottle-a959341b73159008f39a5a9d3ee61abb741492c9.tar.gz |
Added a recipe for keep-alive requests
Diffstat (limited to 'docs')
-rw-r--r-- | docs/recipes.rst | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/docs/recipes.rst b/docs/recipes.rst index f35682f..3ada2f8 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -6,6 +6,7 @@ .. _werkzeug: http://werkzeug.pocoo.org/documentation/dev/debug.html .. _paste: http://pythonpaste.org/modules/evalexception.html .. _pylons: http://pylonshq.com/ +.. _gevent: http://www.gevent.org/ Recipes ============= @@ -101,3 +102,25 @@ or add a WSGI middleware that strips trailing slashes from all URLs:: .. [1] Because they are. See <http://www.ietf.org/rfc/rfc3986.txt> +Keep-alive requests +------------------- + +Several "push" mechanisms like XHR multipart need the ability to write response data without closing the connection in conjunction with the response header "Connection: keep-alive". WSGI does not easily lend itself to this behavior, but it is still possible to do so in Bottle by using the gevent_ async framework. Here is a sample that works with either the gevent_ HTTP server or the paste_ HTTP server (it may work with others, but I have not tried). Just change ``server='gevent'`` to ``server='paste'`` to use the paste_ server. + + from gevent import monkey; monkey.patch_all() + + import time + from bottle import route, run + + @route('/stream') + def stream(): + yield 'START' + time.sleep(3) + yield 'MIDDLE' + time.sleep(5) + yield 'END' + + run(host='0.0.0.0', port=8080, server='gevent') + +If you browse to ``http://localhost:8080/stream``, you should see 'START', 'MIDDLE', and 'END' show up one at a time (rather than waiting 8 seconds to see them all at once). + |