Python · Синтаксис · Просунутий рівень
Асинхронний Python
Пишіть конкурентні програми з `async def`, `await` та `asyncio`. Охоплює корутини, `asyncio.gather`, завдання, `asyncio.Queue` та асинхронні ітератори.
Короткий вступ до теми та пояснення перед вправами (вправи нижче):
Tasks, async for/with, таймаут, конкурентні HTTP-запити
#Поширені помилки, asyncio vs потоки vs multiprocessing, довідник
#Вправи:
Перша корутина з asyncio.run
#import asyncio
async def greet(name: str) -> None:
pass
asyncio.run(greet('Аліса'))
# Привіт, Аліса!
# До побачення, Аліса!
Рішення
import asyncio
async def greet(name: str) -> None:
print(f'Привіт, {name}!')
await asyncio.sleep(0.1)
print(f'До побачення, {name}!')
asyncio.run(greet('Аліса'))
# Привіт, Аліса!
# До побачення, Аліса!
Конкурентні завдання з asyncio.gather
#import asyncio
async def fetch(url: str, delay: float) -> str:
pass
async def main() -> None:
results = await asyncio.gather(
fetch('A', 0.3),
fetch('B', 0.1),
fetch('C', 0.2),
)
for r in results:
print(r)
asyncio.run(main())
Рішення
import asyncio
async def fetch(url: str, delay: float) -> str:
await asyncio.sleep(delay)
return f'Response from {url}'
async def main() -> None:
results = await asyncio.gather(
fetch('A', 0.3),
fetch('B', 0.1),
fetch('C', 0.2),
)
for r in results:
print(r)
asyncio.run(main())
# Response from A
# Response from B
# Response from C
Фонові завдання з create_task
#import asyncio
async def countdown() -> None:
for i in range(3, 0, -1):
print(f'Зворотній відлік: {i}')
await asyncio.sleep(0.1)
async def worker() -> None:
for _ in range(3):
print('Працюємо...')
await asyncio.sleep(0.15)
async def main() -> None:
# створіть завдання та дочекайтесь їх
pass
asyncio.run(main())
Рішення
import asyncio
async def countdown() -> None:
for i in range(3, 0, -1):
print(f'Зворотній відлік: {i}')
await asyncio.sleep(0.1)
async def worker() -> None:
for _ in range(3):
print('Працюємо...')
await asyncio.sleep(0.15)
async def main() -> None:
task1 = asyncio.create_task(countdown())
task2 = asyncio.create_task(worker())
await task1
await task2
asyncio.run(main())
Виробник-споживач з asyncio.Queue
#import asyncio
async def producer(queue: asyncio.Queue) -> None:
pass
async def consumer(queue: asyncio.Queue) -> None:
pass
async def main() -> None:
queue: asyncio.Queue = asyncio.Queue()
await asyncio.gather(producer(queue), consumer(queue))
asyncio.run(main())
Рішення
import asyncio
async def producer(queue: asyncio.Queue) -> None:
for i in range(1, 6):
await queue.put(i)
await asyncio.sleep(0.05)
await queue.put(None) # сигнал завершення
async def consumer(queue: asyncio.Queue) -> None:
while True:
item = await queue.get()
if item is None:
break
print(f'Consuming: {item}')
async def main() -> None:
queue: asyncio.Queue = asyncio.Queue()
await asyncio.gather(producer(queue), consumer(queue))
asyncio.run(main())
Таймаут з asyncio.wait_for
#import asyncio
async def slow_op(seconds: float) -> str:
await asyncio.sleep(seconds)
return 'done'
async def main() -> None:
try:
result = await asyncio.wait_for(slow_op(5), timeout=0.2)
print(result)
except asyncio.TimeoutError:
pass # виведіть повідомлення тут
asyncio.run(main())
# Operation timed out!
Рішення
import asyncio
async def slow_op(seconds: float) -> str:
await asyncio.sleep(seconds)
return 'done'
async def main() -> None:
try:
result = await asyncio.wait_for(slow_op(5), timeout=0.2)
print(result)
except asyncio.TimeoutError:
print('Operation timed out!')
asyncio.run(main())
# Operation timed out!
Асинхронний генератор
#import asyncio
async def async_range(start: int, stop: int, delay: float):
pass
async def main() -> None:
async for value in async_range(0, 4, 0.05):
print(value)
asyncio.run(main())
# 0
# 1
# 2
# 3
Рішення
import asyncio
async def async_range(start: int, stop: int, delay: float):
for i in range(start, stop):
yield i
await asyncio.sleep(delay)
async def main() -> None:
async for value in async_range(0, 4, 0.05):
print(value)
asyncio.run(main())
Асинхронний контекстний менеджер
#import asyncio
import time
class AsyncTimer:
async def __aenter__(self):
pass
async def __aexit__(self, *args):
pass
async def main() -> None:
async with AsyncTimer():
await asyncio.sleep(0.1)
asyncio.run(main())
# Timer started
# Elapsed: 0.10s
Рішення
import asyncio
import time
class AsyncTimer:
async def __aenter__(self):
self._start = time.perf_counter()
print('Timer started')
return self
async def __aexit__(self, *args):
elapsed = time.perf_counter() - self._start
print(f'Elapsed: {elapsed:.2f}s')
async def main() -> None:
async with AsyncTimer():
await asyncio.sleep(0.1)
asyncio.run(main())
Обмеження конкурентності з asyncio.Semaphore
#import asyncio
async def task(n: int, sem: asyncio.Semaphore) -> None:
async with sem:
print(f'Task {n} started')
await asyncio.sleep(0.1)
print(f'Task {n} done')
async def main() -> None:
sem = asyncio.Semaphore(3)
# створіть і запустіть 8 завдань (n від 1 до 8)
pass
asyncio.run(main())
Рішення
import asyncio
async def task(n: int, sem: asyncio.Semaphore) -> None:
async with sem:
print(f'Task {n} started')
await asyncio.sleep(0.1)
print(f'Task {n} done')
async def main() -> None:
sem = asyncio.Semaphore(3)
await asyncio.gather(*(task(n, sem) for n in range(1, 9)))
asyncio.run(main())
Виконання блокуючого коду через run_in_executor
#import asyncio
def blocking_sum(n: int) -> int:
return sum(range(n + 1))
async def async_sum(n: int) -> int:
loop = asyncio.get_running_loop()
# запустіть blocking_sum у пулі потоків
pass
async def main() -> None:
results = await asyncio.gather(async_sum(1000), async_sum(2000))
print(results) # [500500, 2001000]
asyncio.run(main())
Рішення
import asyncio
def blocking_sum(n: int) -> int:
return sum(range(n + 1))
async def async_sum(n: int) -> int:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(None, blocking_sum, n)
async def main() -> None:
results = await asyncio.gather(async_sum(1000), async_sum(2000))
print(results) # [500500, 2001000]
asyncio.run(main())
Координація з asyncio.Event
#import asyncio
async def setter(event: asyncio.Event) -> None:
await asyncio.sleep(0.15)
print('Setting event')
event.set()
async def waiter(event: asyncio.Event) -> None:
pass # зачекайте подію, потім виведіть 'Event received!'
async def main() -> None:
event = asyncio.Event()
await asyncio.gather(setter(event), waiter(event))
asyncio.run(main())
Рішення
import asyncio
async def setter(event: asyncio.Event) -> None:
await asyncio.sleep(0.15)
print('Setting event')
event.set()
async def waiter(event: asyncio.Event) -> None:
print('Waiting for event...')
await event.wait()
print('Event received!')
async def main() -> None:
event = asyncio.Event()
await asyncio.gather(setter(event), waiter(event))
asyncio.run(main())