diff --git a/understanding-asynchronous-programming/example_1.py b/understanding-asynchronous-programming/example_1.py index 0b8529e5ce..2592b0a18f 100644 --- a/understanding-asynchronous-programming/example_1.py +++ b/understanding-asynchronous-programming/example_1.py @@ -4,33 +4,26 @@ def task(name, work_queue): if work_queue.empty(): print(f"Task {name} nothing to do") - else: - while not work_queue.empty(): - count = work_queue.get() - total = 0 - print(f"Task {name} running") - for x in range(count): - total += 1 - print(f"Task {name} total: {total}") + return + + while not work_queue.empty(): + count = work_queue.get() + total = 0 + print(f"Task {name} running") + for _ in range(count): + total += 1 + print(f"Task {name} total: {total}") def main(): - """ - This is the main entry point for the program - """ - # Create the queue of work work_queue = queue.Queue() - - # Put some work in the queue for work in [15, 10, 5, 2]: work_queue.put(work) - # Create some synchronous tasks tasks = [(task, "One", work_queue), (task, "Two", work_queue)] - # Run the tasks - for t, n, q in tasks: - t(n, q) + for task_func, task_name, tasks_queue in tasks: + task_func(task_name, tasks_queue) if __name__ == "__main__": diff --git a/understanding-asynchronous-programming/example_2.py b/understanding-asynchronous-programming/example_2.py index 53c6b8ca27..1e543ad390 100644 --- a/understanding-asynchronous-programming/example_2.py +++ b/understanding-asynchronous-programming/example_2.py @@ -1,42 +1,30 @@ import queue -from builtins import range -def task(name, queue): - while not queue.empty(): - count = queue.get() +def task(name, work_queue): + while not work_queue.empty(): + count = work_queue.get() total = 0 print(f"Task {name} running") - for x in range(count): + for _ in range(count): total += 1 yield print(f"Task {name} total: {total}") def main(): - """ - This is the main entry point for the program - """ - # Create the queue of work work_queue = queue.Queue() - - # Put some work in the queue for work in [15, 10, 5, 2]: work_queue.put(work) - # Create some tasks tasks = [task("One", work_queue), task("Two", work_queue)] - # Run the tasks - done = False - while not done: - for t in tasks: + while tasks: + for current_task in tasks.copy(): try: - next(t) + next(current_task) except StopIteration: - tasks.remove(t) - if len(tasks) == 0: - done = True + tasks.remove(current_task) if __name__ == "__main__": diff --git a/understanding-asynchronous-programming/example_3.py b/understanding-asynchronous-programming/example_3.py index 6a5aa34b4c..381a49bf6d 100644 --- a/understanding-asynchronous-programming/example_3.py +++ b/understanding-asynchronous-programming/example_3.py @@ -4,10 +4,10 @@ from codetiming import Timer -def task(name, queue): +def task(name, work_queue): timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}") - while not queue.empty(): - delay = queue.get() + while not work_queue.empty(): + delay = work_queue.get() print(f"Task {name} running") timer.start() time.sleep(delay) @@ -16,29 +16,19 @@ def task(name, queue): def main(): - """ - This is the main entry point for the program - """ - # Create the queue of work work_queue = queue.Queue() - - # Put some work in the queue for work in [15, 10, 5, 2]: work_queue.put(work) tasks = [task("One", work_queue), task("Two", work_queue)] - # Run the tasks - done = False with Timer(text="\nTotal elapsed time: {:.1f}"): - while not done: - for t in tasks: + while tasks: + for current_task in tasks.copy(): try: - next(t) + next(current_task) except StopIteration: - tasks.remove(t) - if len(tasks) == 0: - done = True + tasks.remove(current_task) if __name__ == "__main__": diff --git a/understanding-asynchronous-programming/example_4.py b/understanding-asynchronous-programming/example_4.py index d95bb62b43..b6703b616f 100644 --- a/understanding-asynchronous-programming/example_4.py +++ b/understanding-asynchronous-programming/example_4.py @@ -14,22 +14,14 @@ async def task(name, work_queue): async def main(): - """ - This is the main entry point for the program - """ - # Create the queue of work work_queue = asyncio.Queue() - - # Put some work in the queue for work in [15, 10, 5, 2]: await work_queue.put(work) - # Run the tasks with Timer(text="\nTotal elapsed time: {:.1f}"): - await asyncio.gather( - asyncio.create_task(task("One", work_queue)), - asyncio.create_task(task("Two", work_queue)), - ) + async with asyncio.TaskGroup() as group: + group.create_task(task("One", work_queue)) + group.create_task(task("Two", work_queue)) if __name__ == "__main__": diff --git a/understanding-asynchronous-programming/example_5.py b/understanding-asynchronous-programming/example_5.py index afea769218..6e3977fc94 100644 --- a/understanding-asynchronous-programming/example_5.py +++ b/understanding-asynchronous-programming/example_5.py @@ -17,37 +17,28 @@ def task(name, work_queue): def main(): - """ - This is the main entry point for the program - """ - # Create the queue of work - work_queue = queue.Queue() + urls = [ + "https://www.google.com", + "https://www.linkedin.com", + "https://www.apple.com", + "https://www.microsoft.com", + "https://www.facebook.com", + "https://x.com", + ] - # Put some work in the queue - for url in [ - "http://google.com", - "http://yahoo.com", - "http://linkedin.com", - "http://apple.com", - "http://microsoft.com", - "http://facebook.com", - "http://twitter.com", - ]: + work_queue = queue.Queue() + for url in urls: work_queue.put(url) tasks = [task("One", work_queue), task("Two", work_queue)] - # Run the tasks - done = False with Timer(text="\nTotal elapsed time: {:.1f}"): - while not done: - for t in tasks: + while tasks: + for current_task in tasks.copy(): try: - next(t) + next(current_task) except StopIteration: - tasks.remove(t) - if len(tasks) == 0: - done = True + tasks.remove(current_task) if __name__ == "__main__": diff --git a/understanding-asynchronous-programming/example_6.py b/understanding-asynchronous-programming/example_6.py index 1f4e3d2810..342bc0379b 100644 --- a/understanding-asynchronous-programming/example_6.py +++ b/understanding-asynchronous-programming/example_6.py @@ -11,36 +11,30 @@ async def task(name, work_queue): url = await work_queue.get() print(f"Task {name} getting URL: {url}") timer.start() - async with session.get(url) as response: - await response.text() + async with asyncio.timeout(10): + async with session.get(url) as response: + await response.text() timer.stop() async def main(): - """ - This is the main entry point for the program - """ - # Create the queue of work - work_queue = asyncio.Queue() + urls = [ + "https://www.google.com", + "https://www.linkedin.com", + "https://www.apple.com", + "https://www.microsoft.com", + "https://www.facebook.com", + "https://x.com", + ] - # Put some work in the queue - for url in [ - "http://google.com", - "http://yahoo.com", - "http://linkedin.com", - "http://apple.com", - "http://microsoft.com", - "http://facebook.com", - "http://twitter.com", - ]: + work_queue = asyncio.Queue() + for url in urls: await work_queue.put(url) - # Run the tasks with Timer(text="\nTotal elapsed time: {:.1f}"): - await asyncio.gather( - asyncio.create_task(task("One", work_queue)), - asyncio.create_task(task("Two", work_queue)), - ) + async with asyncio.TaskGroup() as group: + group.create_task(task("One", work_queue)) + group.create_task(task("Two", work_queue)) if __name__ == "__main__": diff --git a/understanding-asynchronous-programming/requirements.txt b/understanding-asynchronous-programming/requirements.txt index e5f9331f7e..e55d45505d 100644 --- a/understanding-asynchronous-programming/requirements.txt +++ b/understanding-asynchronous-programming/requirements.txt @@ -1,11 +1,3 @@ -aiohttp==3.5.4 -async-timeout==3.0.1 -attrs==19.1.0 -certifi==2019.3.9 -chardet==3.0.4 -codetiming==0.1.2 -idna==2.8 -multidict==4.5.2 -requests==2.21.0 -urllib3==1.24.2 -yarl==1.3.0 +aiohttp==3.14.3 +codetiming==1.4.0 +requests==2.34.2