0
# Task Management
1
2
Core functionality for converting synchronous functions to asynchronous tasks and managing their execution lifecycle.
3
4
## Capabilities
5
6
### Task Decoration
7
8
The primary decorator that converts any function into an asynchronous task that runs in the background using the current pool's configuration.
9
10
```python { .api }
11
def task(callee: Callable[..., Any]) -> Callable[..., Optional[Union[Thread, Process]]]:
12
"""
13
Decorator that converts a function into an asynchronous task.
14
15
This is the main decorator of the library. It wraps any function
16
to make it run asynchronously in the background using the current
17
pool's configuration (threads or processes).
18
19
Args:
20
callee: The function to be made asynchronous
21
22
Returns:
23
Decorated function that returns Thread/Process object or None
24
"""
25
```
26
27
**Usage Example:**
28
29
```python
30
import multitasking
31
import time
32
33
@multitasking.task
34
def process_item(item_id):
35
# Simulate processing time
36
time.sleep(1)
37
return f"Processed item {item_id}"
38
39
# Start multiple tasks
40
for i in range(5):
41
task_obj = process_item(i)
42
print(f"Started task: {task_obj}")
43
44
# Wait for all to complete
45
multitasking.wait_for_tasks()
46
```
47
48
### Task Listing
49
50
Retrieve information about all tasks created by the library.
51
52
```python { .api }
53
def get_list_of_tasks() -> List[Union[Thread, Process]]:
54
"""
55
Retrieve all tasks ever created by this library.
56
57
This includes both currently running tasks and completed ones.
58
Useful for debugging and monitoring task history.
59
60
Returns:
61
List of all Thread/Process objects created by @task decorator
62
63
Note:
64
Completed tasks remain in this list until program termination.
65
"""
66
```
67
68
**Usage Example:**
69
70
```python
71
import multitasking
72
73
@multitasking.task
74
def worker(task_id):
75
time.sleep(1)
76
print(f"Task {task_id} completed")
77
78
# Start some tasks
79
for i in range(3):
80
worker(i)
81
82
# Check all tasks (running and completed)
83
all_tasks = multitasking.get_list_of_tasks()
84
print(f"Total tasks created: {len(all_tasks)}")
85
86
multitasking.wait_for_tasks()
87
88
# All tasks still listed even after completion
89
final_tasks = multitasking.get_list_of_tasks()
90
print(f"Tasks after completion: {len(final_tasks)}")
91
```
92
93
### Active Task Monitoring
94
95
Monitor only the currently running tasks.
96
97
```python { .api }
98
def get_active_tasks() -> List[Union[Thread, Process]]:
99
"""
100
Retrieve only the currently running tasks.
101
102
Filters the complete task list to show only tasks that are still
103
executing. This is more useful than get_list_of_tasks() for
104
monitoring current system load.
105
106
Returns:
107
List of Thread/Process objects that are still running
108
"""
109
```
110
111
**Usage Example:**
112
113
```python
114
import multitasking
115
import time
116
117
@multitasking.task
118
def long_task(duration):
119
time.sleep(duration)
120
print(f"Task completed after {duration}s")
121
122
# Start tasks with different durations
123
long_task(3)
124
long_task(2)
125
long_task(1)
126
127
# Monitor progress
128
while multitasking.get_active_tasks():
129
active_count = len(multitasking.get_active_tasks())
130
total_count = len(multitasking.get_list_of_tasks())
131
completed = total_count - active_count
132
print(f"Progress: {completed}/{total_count} completed")
133
time.sleep(0.5)
134
135
print("All tasks finished!")
136
```