Async Runner - Run async tasks from sync methods
Project description
Async Runner
Async Runner is a minimal Python utility designed to run asynchronous tasks from synchronous code — without worrying about event loop conflicts or lifecycle issues.
Why Async Runner?
In many real-world projects, especially when integrating third-party libraries or legacy synchronous code, you may need to invoke async functions inside a sync context. However, common approaches come with challenges:
asyncio.run()creates a new event loop, which fails if one already exists.- Some libraries require an existing event loop, others fail if one already exists.
- Solutions like
greenletcan be overly complex or unsuitable for certain use cases.
After struggling with these limitations across different projects, this package was built to offer a clean, lightweight, and thread-safe solution by:
✅ Delegating async tasks to a background thread
✅ Maintaining a single, long-running event loop
✅ Making async-in-sync calls as easy as a regular function call
Installation
pip install async2sync
✅ Features
- Simple, one-line usage
- Thread-safe and reusable
- Works in environments with or without existing event loops
- Avoids
RuntimeError: Event loop is closedorThis event loop is already running issues
How It Works
async_runner creates and manages an event loop inside a dedicated background thread. This allows your sync code to safely schedule and await asynchronous tasks, without interfering with the main thread’s context.
Example Usage
import asyncio
from async_runner import run_async
async def async_task():
await asyncio.sleep(1)
return "Async task completed"
def sync_task():
result = run_async(async_task())
print(result)
sync_task()
Output:
Async task completed
Use Cases
- Calling async APIs from FastAPI views or Python Scripts
- Wrapping async logic inside a sync SDK
- Integrating with libraries that inconsistently depend on event loop presence
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file async2sync-0.1.1.tar.gz.
File metadata
- Download URL: async2sync-0.1.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87792e8c43a564bde9d5ae20f60fb41be96aa327fdf9ef98e79bd58b124d5479
|
|
| MD5 |
69ec4d562c4e708a2715639c8c16bcfa
|
|
| BLAKE2b-256 |
6864dc84f04f6e07bd9b91f8cdbe17cface22730de53bfa5c0ced870dc7cad59
|
File details
Details for the file async2sync-0.1.1-py3-none-any.whl.
File metadata
- Download URL: async2sync-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
110ab1fbb404e3c97c1189b3714761b76259653e0d8721881d8d1d6d31dbeda3
|
|
| MD5 |
3f9bf61c49daa73f647910b279a24f9e
|
|
| BLAKE2b-256 |
77bba7ed8b3f02471dcee5e3ef318ac0621cb0995dc2211e0df0cbdc42cf1b3f
|