multitasking: Non-blocking Python methods using decorators

0.0.12 · active · verified Thu Apr 09

MultiTasking is a lightweight Python library, currently at version 0.0.12, designed to convert Python methods into asynchronous, non-blocking methods using simple decorators. It is particularly effective for I/O-bound tasks such as API calls, web scraping, and database queries, enabling concurrent operations without complex manual thread or process management. The library focuses on ease of use and aims for a stable, albeit infrequent, release cadence with a focus on improvements rather than breaking changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `@multitasking.task` decorator to make a function non-blocking. The `fetch_data` calls will run concurrently. `multitasking.wait_for_tasks()` is used to pause the main thread until all decorated tasks have finished executing. This pattern is ideal for I/O-bound operations where the main program shouldn't wait for each individual task to complete.

import multitasking
import time

@multitasking.task
def fetch_data(url_id):
    # Simulate API call or I/O operation
    time.sleep(1)
    print(f"Fetched data from URL {url_id}")
    return f"Data from {url_id}"

if __name__ == "__main__":
    print("Starting tasks...")
    for i in range(5):
        fetch_data(i)

    # Wait for all tasks to complete
    multitasking.wait_for_tasks()
    print("All tasks completed!")

view raw JSON →