-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunked_async.py
More file actions
executable file
·95 lines (70 loc) · 2.51 KB
/
Copy pathchunked_async.py
File metadata and controls
executable file
·95 lines (70 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
'''
Copyright (c) 2024 Godwin Peter .O
Licensed under the MIT License
you may not use this file except in compliance with the License.
https://opensource.org/license/mit
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Godwin peter .O (me@godwin.dev)
Created At: Saturday, 7th Dec 2024
Modified By: Godwin peter .O
Modified At: Sat Dec 07 2024
'''
import asyncio
from random import uniform
chunk_count: int = 5
total_item: int = 43
task_delay: float = 1.25
max_chunk_task_delay: float = 7.5
def get_chunks(items: list[int], step: int) -> list[list[int]]:
"""
Divide a list into smaller chunks of a given size.
Args:
items (list[int]): The list of items to chunk.
step (int): The size of each chunk.
Returns:
list[list[int]]: A list of chunks.
"""
return [items[i:i + step] for i in range(0, len(items), step)]
async def run_chunk(item: int) -> None:
"""
Simulate processing of an individual item with a delay.
Args:
item (int): The item to process.
Returns:
None
"""
await asyncio.sleep(task_delay)
print("Executing item => {0}".format(item))
async def run_async_chunks(chunks: list[int]) -> None:
"""
Asynchronously process a list of items (chunk) with a delay.
Args:
chunks (list[int]): The list of items in the current chunk.
Returns:
None
"""
# A random float for simulating a delay in seconds for demonstration only
delay: float = uniform(0.1, max_chunk_task_delay)
print(f"Processing chunks {0} to {1}, with {delay:.2f}s delay".format(chunks[0], chunks[len(chunks) - 1]))
await asyncio.sleep(delay)
await asyncio.gather(*(run_chunk(item) for item in chunks))
async def main() -> None:
"""
Main function to orchestrate asynchronous chunk processing.
- Divides the list of items into chunks.
- Processes each chunk asynchronously with random delays.
Returns:
None
"""
items: list[int] = list(range(0, total_item))
chunked_items = list(get_chunks(items, chunk_count))
# Main task execution
tasks = [run_async_chunks(i) for i in chunked_items]
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())