question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use the existing runloop for sync calls in async handler

See original GitHub issue

Hi, 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
tomchristiecommented, Aug 1, 2019

I was just trying to make the api client library idiot proof; and also give them the option to use a sync client even in an async application (for whatever reason that might be)

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.

1reaction
yeraydiazdiazcommented, Jul 31, 2019

There is no API to provide the event loop yourself, however there is a loop property in the AsyncioConcurrencyBackend 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

what is the difference between sync and async input source ...
A run loop receives events from two different types of sources. Input sources deliver asynchronous events, usually messages from another thread ...
Read more >
Async/Await: is it possible to start a Task on @MainActor ...
I've tried to run the current RunLoop until the Task gets its first value, and this grants me with a "synchronous" Task start....
Read more >
Run Loops - Apple Developer
Within your loop, you use a run loop object to "run” the event-processing code that receives events and calls the installed handlers.
Read more >
JavaScript loops - how to handle async/await
As you can see “processArray” is async function. But anonymous function that we use for forEach is synchronous. 1. Don't wait for result....
Read more >
Support for running async functions in sync functions
asyncio.run(func(x)) or loop.run_until_complete() ; however when there is a running loop, these calls cannot be used.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found