Skip to content
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

Merged
merged 4 commits into from Oct 17, 2015
Merged

MOTOR-40 Example using aiohttp. #22

merged 4 commits into from Oct 17, 2015

Conversation

ajdavis
Copy link
Member

@ajdavis ajdavis commented Sep 23, 2015

@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

Review on Reviewable

@jettify
Copy link
Contributor

jettify commented Sep 23, 2015

Thanks @ajdavis ! I would put mongo connection to web.Application , it has dict interface designed to hold database connections and any things to share between handlers. For example:

@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

@asvetlov
Copy link
Contributor

Agree with @jettify

@ajdavis
Copy link
Member Author

ajdavis commented Sep 23, 2015

Updated, thanks for your advice. The documentation is updated here:

http://emptysqua.re/aiotthp-example-docs/tutorial-asyncio.html#a-web-application-with-aiohttp

@asvetlov
Copy link
Contributor

Would you add proper server finalization?
It should looks like:

handler = app.make_handler()
f = loop.create_server(handler, '0.0.0.0', 8080)
srv = loop.run_until_complete(f)
main_socket = srv.sockets[0]
print('serving on', srv.sockets[0].getsockname())
try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
main_socket.close()
await handler.finish_connections()
await app.finish()

@asvetlov
Copy link
Contributor

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.

@ajdavis
Copy link
Member Author

ajdavis commented Sep 25, 2015

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?

@ajdavis
Copy link
Member Author

ajdavis commented Sep 25, 2015

@ajdavis
Copy link
Member Author

ajdavis commented Oct 2, 2015

Hey @jettify and @asvetlov I plan to merge this, could you take a final look at my aiohttp server finalization code and tell me if it's correct? Is there no simpler way to do it?

Must stop listening for new connections, before awaiting shutdown on
existing connections.
@ajdavis
Copy link
Member Author

ajdavis commented Oct 16, 2015

Updated to close the listening socket before handler.finish_connections.

@asvetlov
Copy link
Contributor

Looks good

@ajdavis
Copy link
Member Author

ajdavis commented Oct 17, 2015

Thanks!

ajdavis added a commit that referenced this pull request Oct 17, 2015
@ajdavis ajdavis merged commit 58cd779 into master Oct 17, 2015
@ajdavis ajdavis deleted the aiohttp-example branch January 18, 2018 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants