-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsync_IO.py
More file actions
88 lines (75 loc) · 2.92 KB
/
Copy pathAsync_IO.py
File metadata and controls
88 lines (75 loc) · 2.92 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
# =====================================================================
# FILE: Async_IO.py
# DESCRIPTION: Coroutines, async event loops, gathering async functions, and concurrent task execution.
#
# SYNTAX QUICK-REFERENCE:
# import asyncio
#
# async def worker():
# await asyncio.sleep(1)
#
# async def main():
# await asyncio.gather(worker(), worker())
#
# asyncio.run(main())
# =====================================================================
# Async_IO.py
# Reference Guide: Asynchronous Programming using asyncio, async/await, Tasks, and gather
import asyncio
import time
# ==========================================
# 1. CORE CONCEPTS
# ==========================================
# - Synchronous code runs one command at a time, blocking execution.
# - Asynchronous code allows task execution to pause/yield control back to the
# event loop, allowing other tasks to run concurrently during I/O operations.
# - Keywords: 'async def' defines a coroutine, 'await' pauses execution of the coroutine.
print("--- 1. DEFINING & RUNNING COROUTINES ---")
async def main():
print(" Hello...")
await asyncio.sleep(0.05) # Yields control
print(" ...World!")
# Running the event loop (runs the coroutine to completion)
asyncio.run(main())
print()
# ==========================================
# 2. RUNNING CONCURRENT TASKS WITH GATHER
# ==========================================
# asyncio.gather runs multiple coroutines concurrently and collects their results.
print("--- 2. CONCURRENCY WITH asyncio.gather ---")
async def fetch_data(source_id: int, delay: float) -> str:
print(f" Fetching data from Source {source_id}...")
await asyncio.sleep(delay) # Simulate network delay
print(f" Received data from Source {source_id}!")
return f"Data {source_id}"
async def run_gather():
start_time = time.time()
# Execute fetch_data concurrently
results = await asyncio.gather(
fetch_data(1, 0.1),
fetch_data(2, 0.15),
fetch_data(3, 0.05)
)
end_time = time.time()
print(f"All data gathered: {results}")
print(f"Time elapsed: {end_time - start_time:.4f} seconds") # Will be roughly equal to the longest delay (0.15s) rather than the sum (0.3s)
asyncio.run(run_gather())
print()
# ==========================================
# 3. CREATING BACKGROUND TASKS
# ==========================================
# Tasks allow a coroutine to run in the background while other operations continue.
print("--- 3. CREATING BACKGROUND TASKS ---")
async def background_worker():
for i in range(1, 4):
print(f" [Worker] Step {i}")
await asyncio.sleep(0.05)
async def test_task():
# Schedule the worker in the background
task = asyncio.create_task(background_worker())
print(" [Main] Doing some work...")
await asyncio.sleep(0.08)
print(" [Main] Waiting for worker task to complete...")
await task # wait for it to finish
asyncio.run(test_task())
print()