Skip to content

Instantly share code, notes, and snippets.

@swaroopch
Created February 15, 2012 20:25
Show Gist options
  • Save swaroopch/1838783 to your computer and use it in GitHub Desktop.
Save swaroopch/1838783 to your computer and use it in GitHub Desktop.
Receive and close web request ASAP, process data later
→ python server.py
Run after this server starts : curl http://127.0.0.1:5000/?test=1
[2012-02-15 12:24:34,716] [werkzeug] [INFO] * Running on http://127.0.0.1:5000/
[2012-02-15 12:24:34,717] [werkzeug] [INFO] * Restarting with reloader
Run after this server starts : curl http://127.0.0.1:5000/?test=1
[2012-02-15 12:24:36,459] [root] [INFO] Received data ImmutableMultiDict([('test', u'1')])
[2012-02-15 12:24:36,460] [root] [INFO] Closing connection!
[2012-02-15 12:24:36,460] [werkzeug] [INFO] 127.0.0.1 - - [15/Feb/2012 12:24:36] "GET /?test=1 HTTP/1.1" 200 -
[2012-02-15 12:24:36,460] [root] [INFO] In separate process, doing something with data {'test': [u'1']}
#!/usr/bin/env python
'''
Exploration on how to receive incoming data and close the connection ASAP, and
continue processing after the connection is closed.
http://jessenoller.com/code/pycon_jnoller_multiprocessing.pdf
'''
import logging
logging.basicConfig(
level=logging.DEBUG,
format='[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s'
)
import multiprocessing as multi
from flask import Flask, request
app = Flask(__name__)
def do_after_connection_closed(**kwargs):
logging.info("In separate process, doing something with data %s", kwargs)
@app.route("/")
def server():
p = multi.Process(target=do_after_connection_closed, kwargs=(request.args))
p.start()
logging.info("Received data %s", request.args)
logging.info("Closing connection!")
return "Back\n"
if __name__ == '__main__':
print "Run after this server starts : curl http://127.0.0.1:5000/?test=1"
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment