Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions understanding-asynchronous-programming/example_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
28 changes: 8 additions & 20 deletions understanding-asynchronous-programming/example_2.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down
24 changes: 7 additions & 17 deletions understanding-asynchronous-programming/example_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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__":
Expand Down
14 changes: 3 additions & 11 deletions understanding-asynchronous-programming/example_4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
37 changes: 14 additions & 23 deletions understanding-asynchronous-programming/example_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
38 changes: 16 additions & 22 deletions understanding-asynchronous-programming/example_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
14 changes: 3 additions & 11 deletions understanding-asynchronous-programming/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Loading