-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing_11_14.py
38 lines (30 loc) · 1020 Bytes
/
listing_11_14.py
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
"""
Иллюстрация условий
"""
import asyncio
from asyncio import Condition
async def do_work(condition: Condition):
while True:
print("Waiting for condition...")
async with condition:
print("Lock acquired, releasing and waiting for condition...")
await condition.wait()
print("Condition met, acquiring lock again and starting to work...")
await asyncio.sleep(1)
print("Work is done, lock released.")
async def fire_event(condition: Condition):
while True:
await asyncio.sleep(5)
print("Acquire condition before notifying...")
async with condition:
print("Lock acquired, notifying workers...")
condition.notify_all()
print("Workers notified, releasing lock.")
async def main():
condition = Condition()
asyncio.create_task(fire_event(condition))
await asyncio.gather(
do_work(condition),
do_work(condition),
)
asyncio.run(main())