-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Feature Request] Provide a simpler method to run an app locally #690
Description
Right now, there are several examples that run an app locally in sightly different ways (see tests/autobahn/server.py, examples/web_srv.py, examples/web_ws.py, http://aiohttp.readthedocs.org/en/stable/#getting-started and http://aiohttp.readthedocs.org/en/stable/web.html#run-a-simple-web-server). And if you want to start testing your app, you need to copy that (boilerplate) code snippet.
I propose adding a utility function, or a method to run and app locally in a simpler way.
Right now my utility function is as follows:
def run_app(app, host='0.0.0.0', port=8080):
"""Run an app locally."""
loop = asyncio.get_event_loop()
handler = app.make_handler()
f = loop.create_server(handler, host, port)
srv = loop.run_until_complete(f)
if host == '0.0.0.0':
host = '127.0.0.1'
print(' * Running on http://{host}:{port}/ (Press CTRL+C to quit)'.format(
host=host, port=port))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
loop.run_until_complete(handler.finish_connections(1.0))
srv.close()
loop.run_until_complete(srv.wait_closed())
loop.run_until_complete(app.finish())
loop.close()alternatively, this function could be a method of an app.