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.
pip install multitaskingVerified import paths — ran on the pinned version, not inferred.
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.
For CPU-bound tasks, consider configuring `multitasking` to use `multiprocessing.Process` instead of `threading.Thread` by using `multitasking.set_engine('process')`. Be aware that multiprocessing introduces higher overhead for startup and inter-process communication.Ensure that `multitasking` is primarily used for I/O-bound workloads to maximize its benefits. For CPU-bound tasks requiring true parallelism, explicitly switch the execution engine to multiprocessing using `multitasking.set_engine('process')` before launching tasks, if the overhead is acceptable for your application.Adjust the maximum number of concurrent tasks using `multitasking.set_max_threads(N)` to match your application's specific needs and resource availability. Experiment with different values to find the sweet spot for your workload.
Install the library using pip: `pip install multitasking`
Ensure the decorator is spelled correctly and used as `@multitasking.task` above the function definition. For example:
```python
import multitasking
import time
@multitasking.task
def my_task():
time.sleep(1)
print('Task finished')
my_task()
multitasking.wait_for_tasks()
```Ensure that `multitasking.wait_for_tasks()` is called only after all desired tasks have been initiated, allowing them to run concurrently in the background. For example:
```python
import multitasking
import time
@multitasking.task
def my_task(task_id):
print(f'Starting task {task_id}')
time.sleep(2)
print(f'Finished task {task_id}')
for i in range(3):
my_task(i) # Initiate tasks without waiting
print('All tasks initiated, waiting for completion...')
multitasking.wait_for_tasks() # Wait for all initiated tasks to finish
print('All tasks completed')
```No dependency data recorded yet.