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

aiohttp.ClientSession usage with Django #3357

Closed
connexion2000 opened this issue Oct 22, 2018 · 8 comments
Closed

aiohttp.ClientSession usage with Django #3357

connexion2000 opened this issue Oct 22, 2018 · 8 comments
Labels

Comments

@connexion2000
Copy link

Hi,
I would like to use ClientSession inside my Django app. As documentations says I should have one ClientSession instance per server lifetime. My question is: where should ClientSession's initialization take place? How to perform cleanup then?

My current solution is myhttp.py module:

import aiohttp
import asyncio
import atexit


loop = asyncio.new_event_loop()
conn = aiohttp.TCPConnector(loop=loop)
client_session = aiohttp.ClientSession(connector=conn)


@atexit.register
def cleanup():
    print('Cleanup')
    loop.run_until_complete(client_session.close())
    loop.close()

and in my whole app I just do:

from myhttp import client_session

but I don't know if it is proper approach. What is more it seems that during server shutdown cleanup function is not performed, because nothing is printed to stdout.

@asvetlov
Copy link
Member

aiohttp was never intended to work with synchronous WSGI application.
Nobody can prevent you to do it though -- it is the Open Source software.
But please keep in mind that next aiohttp releases may break the found approach.

@jettify
Copy link
Member

jettify commented Oct 22, 2018

Agreed, I believe it is better to rethink problem and do not use aiohttp in Django in first place.

Depends on workload, may be just submit work to message queue from Django and pick it with async worker, where you can use aiohttp properly?

@connexion2000
Copy link
Author

connexion2000 commented Oct 22, 2018

If I have to check health of 3-5 servers in parallel (timeout 1 sex per server) during 1 django request, so aiohttp isn't good choice?

@asvetlov
Copy link
Member

asyncio is not a good choice if you want to call it from a synchronous WSGI application.

@jettify
Copy link
Member

jettify commented Oct 22, 2018

You can create simple aiohttp server for this purposes and use whatever timeout you want without blocking other requests :)

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #58 (aiohttp.HttpClient), #2036 (ClientSession created in aiohttp.request() remains unclosed), #2473 (Replace aiohttp.ClientSession with aiohttp.create_session()), #2867 (aiohttp.ClientSession reconnect ), and #919 (Suggesting aiohttp.ClientSession.abort()).

@connexion2000
Copy link
Author

Thanks for clarification.

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants