Skip to content

Allow using Client in an async environment - #513

Closed
florimondmanca wants to merge 1 commit into
masterfrom
feat/asyncio-client-sync-in-async
Closed

florimondmanca wants to merge 1 commit into
masterfrom
feat/asyncio-client-sync-in-async

Conversation

@florimondmanca

@florimondmanca florimondmanca commented Nov 7, 2019 •

Copy link
Copy Markdown
Contributor

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

florimondmanca commented Nov 8, 2019 •

Copy link
Copy Markdown
Contributor Author

@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
florimondmanca force-pushed the feat/asyncio-client-sync-in-async branch from bf7f207 to b651380 Compare November 8, 2019 21:14
@florimondmanca

Copy link
Copy Markdown
Contributor 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
Kludex referenced this pull request in pydantic/httpx2 May 11, 2026
Fixes #512

Co-authored-by: Tom Christie <tom@tomchristie.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Incompatible with jupyter

1 participant