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

Allow using Client in an async environment #513

Closed
wants to merge 1 commit into from

Conversation

florimondmanca
Copy link
Member

@florimondmanca florimondmanca commented Nov 7, 2019

Fixes #508

This is an attempt at handling the situation in which users try to use httpx.get() or httpx.Client() in an async environment that has a running loop (e.g. in a Jupyter notebook).

I documented several potential caveats in the form of code comments.

I still need to verify this actually solves the Jupyter use case, though the tests give me hope that it does. :-)

@florimondmanca
Copy link
Member Author

florimondmanca commented Nov 8, 2019

@encode/httpx-maintainers OK, I think I've got something that passes the tests. Basically we're checking if the event loop is running and when relevant run the coroutine in a worker thread instead. This thing is inspired by asgiref's AsyncToSync utility. Definitely needs some cleanup, but at least it seems to work. Also todo: handle shutdown of the threadpool executor (via a new backend.close() method, probably).

@florimondmanca
Copy link
Member Author

Closing as I don't think this will make it, probably in favor of a SyncBackend (unless #522 is accepted) - see #508 (comment).

@florimondmanca florimondmanca deleted the feat/asyncio-client-sync-in-async branch November 12, 2019 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incompatible with jupyter
1 participant