Skip to main content

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 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 async-runner

✅ 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async2sync-0.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async2sync-0.1.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file async2sync-0.1.0.tar.gz.

File metadata

  • Download URL: async2sync-0.1.0.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

Hashes for async2sync-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7a590b4d56c537194bef707f045b86f6d5737bee59a9c32b4028ea3c3975c08a
MD5 49f1cd4a49511d49f3465b5a3eff5a1a
BLAKE2b-256 b42368bb0b30498476a0a63cba05cf09bd922eb3061c6fc245ae60af357273ef

See more details on using hashes here.

File details

Details for the file async2sync-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: async2sync-0.1.0-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

Hashes for async2sync-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e99473f55e4dabad31c2472d218cb351b19d15b39ee74f0c16ec24382384683
MD5 23fed708beacaa736df2a03788257ac1
BLAKE2b-256 df76ec16e092481fd9c08db828e884e4b4414eb3f77d0d6b46603a80f758540e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page