Use the existing runloop for sync calls in async handler
See original GitHub issueHi, this library looks awesome and I really want to use it; but I seem to have hit a blocker and can’t seem to find a solution in the docs.
TL;DR: Is there any way to provide our own runloop when using the sync
calls as looking through the source the sync calls are actually using the async request call.
Basically I’m writing a client api that provides 2 interfaces; an async and sync interface.
The issue that i am having is that if I try to use the sync methods with an existing runloop (provided by Sanic in this scenario), it complains with the exception: Cannot run the event loop while another loop is running
.
Essentially I’m just calling:
import httpx
...
class SomeClass(object):
def get_some_url(self):
r = httpx.get('some url')
note that nothing here is async; apart from the fact that the .get_some_url
call is made from within an async function like so:
import SomeClass
async def handler(request):
o = SomeClass()
o.get_some_url()
This works fine when using the async calls like this:
import httpx
...
class SomeClass(object):
async def get_some_url(self):
client = httpx.AsyncClient()
r = await client.get('some url')
import SomeClass
async def handler(request):
o = SomeClass()
await o.get_some_url()
So I was just wondering if its possible to provide our own runloop when using the sync calls? Sanic uses uvloop internally, would be nice to be able to just pass that straight in somehow when constructing the httpx
object, or setting it as a property after import.
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:21 (13 by maintainers)
Top GitHub Comments
I guess this is the core bit. Making the library robust here ought to mean raising a nice clean, explanatory error when it’s used in a broken way, rather than allowing that to pass silently.
There is no API to provide the event loop yourself, however there is a
loop
property in theAsyncioConcurrencyBackend
that checks an existing private_loop
instance variable you can potentially set to the Sanic event loop.We should consider adding this as a feature though.