# How to call async lib function from sync library?

**URL:** <https://discuss.python.org/t/how-to-call-async-lib-function-from-sync-library/65288>\
**Category:** Async-SIG\
**Created:** [September 28, 2024, 1:15am UTC](https://discuss.python.org/t/how-to-call-async-lib-function-from-sync-library/65288 "2024-09-28T01:15:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [September 28, 2024, 1:15am UTC](https://discuss.python.org/t/how-to-call-async-lib-function-from-sync-library/65288/1 "2024-09-28T01:15:52Z")

</div>

Hey folks,

Similar to: [Calling coroutines from sync code](https://discuss.python.org/t/calling-coroutines-from-sync-code/23027)

I have a sync library running from a sync main, that has to call a few async coroutines. Optimally, I don’t want to even really create a asyncio loop, I just want to call the coroutines as if they were regular functions, and just run them to completion.

Is this possible?

I’ve tried:

Using `asyncio.run(<coroutine>)` I get something like:

```python
...
RuntimeError: no running event loop

During handling of the above exception, another exception occurred:

...

File C:\Python311\Lib\site-packages\aiohttp\helpers.py:660, in TimeoutHandle.start(self)
    658 if timeout >= self._ceil_threshold:
    659 when = ceil(when)
--> 660 return self._loop.call_at(when, self. __call__ )
    661 else:
    662 return None

File C:\Python311\Lib\asyncio\base_events.py:737, in BaseEventLoop.call_at(self, when, callback, context, *args)
    735 if when is None:
    736 raise TypeError("when cannot be None")
--> 737 self._check_closed()
    738 if self._debug:
    739 self._check_thread()

File C:\Python311\Lib\asyncio\base_events.py:519, in BaseEventLoop._check_closed(self)
    517 def _check_closed(self):
    518 if self._closed:
--> 519 raise RuntimeError('Event loop is closed')

RuntimeError: Event loop is closed

```

I’ve tried using the recipe from [Calling coroutines from sync code - #2 by srittau](https://discuss.python.org/t/calling-coroutines-from-sync-code/23027/2)… but get a similar error.

What is the ‘most correct’ way to run a given asyncio coroutine synchronously? Should I allocate a loop/run till complete then stop the loop each time?

Note: I don’t want to convert the rest of my code to be async code. There must be a simple way to just use async functions (as if they are sync) from sync functions.

---

<div class="post-metadata">

**Author:** ![mikeshardmind](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mikeshardmind/32/14381_2.png) [@mikeshardmind](https://discuss.python.org/u/mikeshardmind)\
**Post date:** [September 28, 2024, 3:05am UTC](https://discuss.python.org/t/how-to-call-async-lib-function-from-sync-library/65288/2 "2024-09-28T03:05:21Z")

</div>

> [@csm10495](#):
>
> What is the ‘most correct’ way to run a given asyncio coroutine synchronously? Should I allocate a loop/run till complete then stop the loop each time?

That depends on a few factors, so there’s not an unconditional “most correct” way. It almost seems like the most correct way here would be to implement a synchronous version of the function and not have a function coloring problem to begin with since your application is primarily synchronous by description and you want to run the coroutine synchronously.

`asyncio.run` should work fine if the intent is a one shot coroutine and no persistent asyncio objects that bind to a loop (`asyncio.run` recreates a unique event loop per use), the error posted looks more like what I would expect from the recipe you linked, as that recipe requires not only an event loop, but a running one. (`asyncio.get_running_loop`)

Depending on the specific semantics you want and if all your objects are threadsafe, you might want something like this: [async-utils/async\_utils/bg\_loop.py at main · mikeshardmind/async-utils · GitHub](https://github.com/mikeshardmind/async-utils/blob/main/async_utils/bg_loop.py) which can be used to run all the coroutines of your program in a background loop, and can be set up to block on them finishing or not depending on intent.

---

<div class="post-metadata">

**Author:** ![csm10495](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/csm10495/32/9731_2.png) [@csm10495](https://discuss.python.org/u/csm10495)\
**Post date:** [September 28, 2024, 3:19am UTC](https://discuss.python.org/t/how-to-call-async-lib-function-from-sync-library/65288/3 "2024-09-28T03:19:05Z")

</div>

The library I’m using unfortunately only provides an async API ☹. I’ll double check later, but I believe a similar error happens in both cases.

---

<div class="post-metadata">

**Author:** ![zuo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuo/32/11039_2.png) [@zuo](https://discuss.python.org/u/zuo)\
**Post date:** [October 7, 2024, 6:59pm UTC](https://discuss.python.org/t/how-to-call-async-lib-function-from-sync-library/65288/4 "2024-10-07T18:59:20Z")

</div>

You may want to use an [`asyncio.Runner`](https://docs.python.org/3/library/asyncio-runner.html#runner-context-manager).
