multitasking: Non-blocking Python methods using decorators
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
- gotcha Python's Global Interpreter Lock (GIL) limits true parallelism for CPU-bound tasks. While `multitasking` uses threads by default to achieve concurrency, CPU-intensive tasks will not run in parallel across multiple CPU cores due to the GIL, potentially leading to performance degradation from context switching rather than gains.
- gotcha By default, `multitasking` uses Python's `threading` module, which is efficient for I/O-bound operations (e.g., network requests, file I/O) where threads spend most of their time waiting for external resources. However, it's not optimal for CPU-bound tasks due to the GIL.
- gotcha The default maximum number of threads is typically based on the number of CPU cores. While `multitasking` automatically manages a pool, this default might not be optimal for all scenarios, especially when dealing with a very high number of short-lived I/O-bound tasks.
Install
-
pip install multitasking
Imports
- multitasking
import multitasking
- task
from multitasking import task
- wait_for_tasks
from multitasking import wait_for_tasks
Quickstart
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!")