$ pip install falcon
$ python3 app.py
alternatively with Gunicorn (for HTTP/1.1):
$ pip install gunicorn
$ gunicorn app:app
| from asyncio import Future, Task, coroutine as async, sleep | |
| from random import randint | |
| def retrieve(*uris, callback=None): | |
| res = Future() | |
| responses = [] | |
| def conclude(response): | |
| responses.append(response) | |
| if callback: | |
| # The socket being written needs bytes not strings | |
| callback(response.encode('utf-8')) | |
| if len(responses) == len(uris): | |
| res.set_result(responses) | |
| for uri in uris: | |
| task = Task(fetch(uri)) | |
| task.add_done_callback(lambda task: conclude(task.result())) | |
| return res | |
| @async | |
| def fetch(uri): | |
| print("retrieving %s..." % uri) | |
| lag = yield from _http() | |
| lag = lag * 1000 | |
| print("downloaded %s in %d ms" % (uri, lag)) | |
| return "[%d ms] %s\n" % (lag, uri) | |
| @async | |
| def _http(): | |
| lag = randint(2, 9) # delay long enough to notice | |
| yield from sleep(lag) | |
| return lag |
| import asyncio | |
| from wsgiref import simple_server | |
| import aggregator | |
| def on_get(environ, start_response): | |
| """RAW WSGI for the win!""" | |
| uris = ("http://example.org/foo", "http://example.org/bar", | |
| "http://example.org/baz") | |
| writeable = start_response('200 OK', []) | |
| service_requests = aggregator.retrieve(*uris, callback=writeable) | |
| loop = asyncio.get_event_loop() | |
| loop.run_until_complete(service_requests) | |
| # despite us using writeable we need to return an empty iterator | |
| return [] | |
| app = on_get | |
| if __name__ == "__main__": | |
| server = simple_server.make_server("localhost", 8000, app) | |
| server.serve_forever() |