New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MOTOR-40 Example using aiohttp. #22
Conversation
Thanks @ajdavis ! I would put mongo connection to @asyncio.coroutine
def create_example_server(loop):
app = web.Application(loop=loop)
db = yield from setup_db()
app['db'] = db
app.router.add_route('GET', '/pages/{page_name}', page)
srv = yield from loop.create_server(app.make_handler(), '127.0.0.1', 8080)
return srv and then in handler: @asyncio.coroutine
def page(request):
db = request.app['db']
page_name = request.match_info.get('page_name')
document = yield from db.pages.find_one(page_name)
... see also example in docs: http://aiohttp.readthedocs.org/en/latest/web_reference.html#application |
Agree with @jettify |
Updated, thanks for your advice. The documentation is updated here: http://emptysqua.re/aiotthp-example-docs/tutorial-asyncio.html#a-web-application-with-aiohttp |
Would you add proper server finalization?
|
The main idea for finalization is: close passive (accepting) socket and wait for finishing all current connections. For streaming/websockets user should do more work. |
Updated. The docs are regenerated here: http://emptysqua.re/aiotthp-example-docs/tutorial-asyncio.html#a-web-application-with-aiohttp I tried to package all the shutdown steps into a coroutine, did I do that correctly? |
Here is the actual code: https://github.com/mongodb/motor/blob/aiohttp-example/doc/examples/aiohttp_example.py#L57 |
Must stop listening for new connections, before awaiting shutdown on existing connections.
Updated to close the listening socket before |
Looks good |
Thanks! |
MOTOR-40 Example using aiohttp.
@asvetlov, thanks to you and @jettify, Motor 0.5 with asyncio is close to release. Could you review this example application that I plan to include in the tutorial?
The docs are rendered here:
http://emptysqua.re/aiotthp-example-docs/tutorial-asyncio.html#a-web-application-with-aiohttp