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

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.1.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.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

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

Hashes for async2sync-0.1.1.tar.gz
Algorithm Hash digest
SHA256 87792e8c43a564bde9d5ae20f60fb41be96aa327fdf9ef98e79bd58b124d5479
MD5 69ec4d562c4e708a2715639c8c16bcfa
BLAKE2b-256 6864dc84f04f6e07bd9b91f8cdbe17cface22730de53bfa5c0ced870dc7cad59

See more details on using hashes here.

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

Hashes for async2sync-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 110ab1fbb404e3c97c1189b3714761b76259653e0d8721881d8d1d6d31dbeda3
MD5 3f9bf61c49daa73f647910b279a24f9e
BLAKE2b-256 77bba7ed8b3f02471dcee5e3ef318ac0621cb0995dc2211e0df0cbdc42cf1b3f

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