Save work
parent
8562876fdf
commit
bcc92a8ec7
@ -0,0 +1,23 @@
|
||||
# Real Python AsyncIO Walkthrough
|
||||
|
||||
https://realpython.com/async-io-python/
|
||||
|
||||
## Rules of Async Coroutines
|
||||
|
||||
```python
|
||||
async def f(x):
|
||||
y = await z(x) # OK - `await` and `return` allowed in coroutines
|
||||
return y
|
||||
|
||||
async def g(x):
|
||||
yield x # OK - this is an async generator
|
||||
|
||||
async def m(x):
|
||||
yield from gen(x) # No - SyntaxError
|
||||
|
||||
def m(x):
|
||||
y = await z(x) # Still no - SyntaxError (no `async def` here)
|
||||
return y
|
||||
```
|
||||
|
||||
An awaitable object is either (1) another coroutine or (2) an object defining an .__await__() dunder method that returns an iterator.
|
@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python3
|
||||
# countasync.py
|
||||
|
||||
import asyncio
|
||||
|
||||
|
||||
async def count():
|
||||
print("One")
|
||||
await asyncio.sleep(1)
|
||||
print("Two")
|
||||
|
||||
|
||||
async def main():
|
||||
await asyncio.gather(count(), count(), count())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import time
|
||||
|
||||
s = time.perf_counter()
|
||||
asyncio.run(main())
|
||||
elapsed = time.perf_counter() - s
|
||||
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
|
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python3
|
||||
# countsync.py
|
||||
|
||||
import time
|
||||
|
||||
|
||||
def count():
|
||||
print("One")
|
||||
time.sleep(1)
|
||||
print("Two")
|
||||
|
||||
|
||||
def main():
|
||||
for _ in range(3):
|
||||
count()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
s = time.perf_counter()
|
||||
main()
|
||||
elapsed = time.perf_counter() - s
|
||||
print(f"{__file__} executed in {elapsed:0.2f} seconds.")
|
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
# rand.py
|
||||
|
||||
import asyncio
|
||||
import random
|
||||
|
||||
# ANSI colors
|
||||
c = (
|
||||
"\033[0m", # End of color
|
||||
"\033[36m", # Cyan
|
||||
"\033[91m", # Red
|
||||
"\033[35m", # Magenta
|
||||
)
|
||||
|
||||
|
||||
async def makerandom(idx: int, threshold: int = 6) -> int:
|
||||
print(c[idx + 1] + f"Initiated makerandom({idx}).")
|
||||
i = random.randint(0, 10)
|
||||
while i <= threshold:
|
||||
print(c[idx + 1] + f"makerandom({idx}) == {i} too low; retrying.")
|
||||
await asyncio.sleep(idx + 1)
|
||||
i = random.randint(0, 10)
|
||||
print(c[idx + 1] + f"---> Finished: makerandom({idx}) == {i}" + c[0])
|
||||
return i
|
||||
|
||||
|
||||
async def main():
|
||||
res = await asyncio.gather(*(makerandom(i, 10 - i - 1) for i in range(3)))
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
random.seed(444)
|
||||
r1, r2, r3 = asyncio.run(main())
|
||||
print()
|
||||
print(f"r1: {r1}, r2: {r2}, r3: {r3}")
|
Loading…
Reference in New Issue