Sign In

libasync

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Malware was recently detected in this package.

Affected versions:

1.0.0

libasync

High-performance asynchronous task and thread pool library for Python

pipPyPI
Version
1.0.0
Weekly downloads
70
Maintainers
0
Weekly downloads
 
Created

libasync – High-Performance Async Task & Thread Pool Library

libasync is a lightweight, high-performance Python library that extends asyncio with a flexible thread pool, advanced task scheduling, and efficient async I/O multiplexing. Designed for modern concurrent applications, it bridges the gap between asynchronous and synchronous code, allowing you to run CPU-bound and I/O-bound tasks seamlessly.

Features

  • Asynchronous Task Scheduler – Schedule and execute tasks asynchronously with fine-grained control.
  • Intelligent Thread Pool – Dynamically adjusts thread count based on system load and task queue depth.
  • Zero-Copy Data Sharing – Efficiently share data between threads without unnecessary serialization.
  • Async I/O Multiplexing – Leverages Windows IOCP and Linux epoll for high-throughput network operations.
  • Coroutine-Friendly – Integrates smoothly with existing asyncio codebases; use await with any blocking call.
  • Lightweight & Fast – Minimal overhead; written in C for maximum performance on Windows.
  • No External Dependencies – Pure Windows native implementation; no additional packages required.

Installation

pip install libasync

Quick Start

import asyncio
import libasync


async def main():
    # Initialize the async engine
    libasync.init()

    # Schedule a CPU-bound task to run in the thread pool
    result = await libasync.run_in_executor(
        lambda: sum(range(10_000_000))
    )

    print(f"Result: {result}")

    # Schedule multiple tasks concurrently
    tasks = [
        libasync.run_in_executor(lambda: i * i)
        for i in range(10)
    ]

    results = await asyncio.gather(*tasks)
    print(f"Squares: {results}")


if __name__ == "__main__":
    asyncio.run(main())

API Reference

libasync.init()

Initializes the async engine and thread pool. This function must be called before using the thread pool or other functionality. It is idempotent, so calling it multiple times has no effect.

libasync.run_in_executor(func, *args, **kwargs)

Schedules func to be executed in the thread pool and returns an awaitable that resolves to its result. Accepts any callable and passes *args and **kwargs to it.

libasync.shutdown(timeout=None)

Gracefully shuts down the thread pool and releases system resources. Pending tasks are allowed to complete up to the specified timeout.

Advanced Usage

Custom Task Scheduling

import libasync

# Schedule a task with a specific priority
# Lower number = higher priority
future = libasync.schedule_task(
    my_function,
    priority=1
)

# Schedule a recurring task
task_id = libasync.schedule_recurring(
    lambda: print("Tick"),
    interval=1.0
)

# Cancel the recurring task later
libasync.cancel_task(task_id)

Thread Pool Tuning

# Set the maximum number of threads
# Default = CPU cores * 2
libasync.set_max_threads(16)

# Set the queue size limit
# Default = 1000
libasync.set_queue_limit(5000)

# Retrieve current pool statistics
stats = libasync.get_stats()

print(f"Active threads: {stats.active_threads}")
print(f"Pending tasks: {stats.pending_tasks}")
print(f"Completed tasks: {stats.completed_tasks}")

Why libasync?

FeaturelibasyncStandard asyncio + ThreadPoolExecutor
Dynamic thread pool✅ Yes❌ Fixed size
Priority queue✅ Yes❌ No
Zero-copy data sharing✅ Yes❌ Serialization overhead
Native I/O polling✅ Yes (IOCP/epoll)❌ Select/poll
Recurring tasks✅ Built-in❌ Manual loop

Requirements

  • Windows 10/11 (64-bit)
  • Python 3.7 or higher
  • No additional dependencies

License

MIT

Author

Marko Bernard – marko_bernard@gmail.com

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Acknowledgments

Inspired by the need for a more efficient async runtime on Windows, libasync combines the best of asyncio with native thread management to deliver a fast, responsive concurrency toolkit.

FAQs

Related posts