Skip to main content

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 greenlet can 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 closed or This 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

Metadata

Release files for async2sync 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async2sync 0.1.1
File Size Uploaded
async2sync-0.1.1.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for async2sync 0.1.1
File Interpreter ABI Platform
async2sync-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / async2sync-0.1.1.tar.gz

Download URL async2sync-0.1.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
87792e8c43a564bde9d5ae20f60fb41be96aa327fdf9ef98e79bd58b124d5479
BLAKE2b-256 checksum
How to use checksums
6864dc84f04f6e07bd9b91f8cdbe17cface22730de53bfa5c0ced870dc7cad59
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release files / async2sync-0.1.1-py3-none-any.whl

Download URL async2sync-0.1.1-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
110ab1fbb404e3c97c1189b3714761b76259653e0d8721881d8d1d6d31dbeda3
BLAKE2b-256 checksum
How to use checksums
77bba7ed8b3f02471dcee5e3ef318ac0621cb0995dc2211e0df0cbdc42cf1b3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page