Skip to content

Commit eadacf7

Browse files
committed
Added appropriate docstrings
1 parent 4a25957 commit eadacf7

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

chunked_async.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,43 @@
2727
max_chunk_task_delay: float = 7.5
2828

2929
def get_chunks(items: list[int], step: int) -> list[list[int]]:
30+
"""
31+
Divide a list into smaller chunks of a given size.
32+
33+
Args:
34+
items (list[int]): The list of items to chunk.
35+
step (int): The size of each chunk.
36+
37+
Returns:
38+
list[list[int]]: A list of chunks.
39+
"""
40+
3041
return [items[i:i + step] for i in range(0, len(items), step)]
3142

3243
async def run_chunk(item: int) -> None:
33-
await asyncio.sleep(task_delay)
34-
print("Executing item => {0}".format(item))
44+
"""
45+
Simulate processing of an individual item with a delay.
46+
47+
Args:
48+
item (int): The item to process.
49+
50+
Returns:
51+
None
52+
"""
53+
await asyncio.sleep(task_delay)
54+
print("Executing item => {0}".format(item))
3555

3656
async def run_async_chunks(chunks: list[int]) -> None:
57+
"""
58+
Asynchronously process a list of items (chunk) with a delay.
59+
60+
Args:
61+
chunks (list[int]): The list of items in the current chunk.
62+
63+
Returns:
64+
None
65+
"""
66+
3767
# A random float for simulating a delay in seconds for demonstration only
3868
delay: float = uniform(0.1, max_chunk_task_delay)
3969

@@ -43,6 +73,16 @@ async def run_async_chunks(chunks: list[int]) -> None:
4373
await asyncio.gather(*(run_chunk(item) for item in chunks))
4474

4575
async def main() -> None:
76+
"""
77+
Main function to orchestrate asynchronous chunk processing.
78+
79+
- Divides the list of items into chunks.
80+
- Processes each chunk asynchronously with random delays.
81+
82+
Returns:
83+
None
84+
"""
85+
4686
items: list[int] = list(range(0, total_item))
4787
chunked_items = list(get_chunks(items, chunk_count))
4888

@@ -51,5 +91,5 @@ async def main() -> None:
5191
await asyncio.gather(*tasks)
5292

5393

54-
asyncio.run(main())
55-
94+
if __name__ == "__main__":
95+
asyncio.run(main())

0 commit comments

Comments
 (0)