-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b03611
commit 4e86a07
Showing
4 changed files
with
41 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import asyncio | ||
|
||
|
||
async def compute_square(number: int) -> int: | ||
task = asyncio.current_task() | ||
assert task is not None | ||
print(f"Task {task.get_name()} is processing number {number}") | ||
await asyncio.sleep(2) | ||
return number * number | ||
|
||
|
||
async def sequential(numbers: list[int]) -> list[int]: | ||
results = [] | ||
start = asyncio.get_event_loop().time() | ||
for number in numbers: | ||
result = await compute_square(number) | ||
results.append(result) | ||
finish = asyncio.get_event_loop().time() | ||
print(f"Sequential processing time: {finish - start} seconds") | ||
return results | ||
|
||
|
||
async def concurrent(numbers: list[int]) -> list[int]: | ||
start = asyncio.get_event_loop().time() | ||
tasks = [compute_square(number) for number in numbers] | ||
results = await asyncio.gather(*tasks) | ||
finish = asyncio.get_event_loop().time() | ||
print(f"Concurrent processing time: {finish - start} seconds") | ||
return results | ||
|
||
|
||
async def main() -> None: | ||
numbers = [1, 2, 3, 4, 5] | ||
print("Running sequentially:") | ||
await sequential(numbers) | ||
print("Running concurrently:") | ||
await concurrent(numbers) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Empty file.
This file was deleted.
Oops, something went wrong.