-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasyncioExample.py
More file actions
93 lines (68 loc) · 2.12 KB
/
asyncioExample.py
File metadata and controls
93 lines (68 loc) · 2.12 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
89
90
91
92
"""
asyncio is ideal for I/O-bound tasks, such as handling network requests, reading and writing files, and working with databases. It allows multiple tasks to run concurrently within a single thread, which is efficient for I/O-bound operations.
Processes are ideal for CPU-bound tasks because they can run in parallel on multiple CPU cores without being limited by the GIL.
Threads are suitable for I/O-bound tasks but can also be used for CPU-bound tasks. However, due to the Global Interpreter Lock (GIL) in CPython, threads are not the best choice for CPU-bound tasks in Python, as only one thread can execute Python bytecode at a time.
"""
import multiprocessing
def print_numbers():
for i in range(5):
print(i)
process = multiprocessing.Process(target=print_numbers)
process.start()
process.join()
import threading
def print_numbers():
for i in range(5):
print(i)
thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()
import asyncio
class AsyncContextManager:
async def __aenter__(self):
print("Entering context manager")
return self
async def __aexit__(self, exc_type, exc_value, traceback):
print("Exiting context manager")
return False
async def main():
async with AsyncContextManager():
print("Inside context manager")
asyncio.run(main())
async def say_hello():
print("Hello")
await asyncio.sleep(1)
print("World")
async def main():
print("Starting")
await say_hello()
print("Finished")
# Run the main coroutine
asyncio.run(main())
# Output:
# Starting
# Hello
# (pause for 1 second)
# World
# Finished
async def fetch_data():
print("Fetching data...")
await asyncio.sleep(2)
print("Data fetched")
async def process_data():
print("Processing data...")
await asyncio.sleep(1)
print("Data processed")
async def main():
task1 = asyncio.create_task(fetch_data())
task2 = asyncio.create_task(process_data())
await task1
await task2
asyncio.run(main())
# Output:
# Fetching data...
# Processing data...
# (pause for 1 second)
# Data processed
# (pause for 1 second)
# Data fetched