-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.py
56 lines (33 loc) · 990 Bytes
/
cond.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# coding: utf-8
import random
from simple_reactor import get_event_loop
loop = get_event_loop()
cond = loop.create_condition()
stack = []
async def func1():
while 1:
async with cond:
print("func1 locked")
while not stack:
await cond.wait()
assert cond.locked()
assert len(stack) != 0
stack.clear()
cond.notify()
print("func1 release")
async def func2():
while 1:
async with cond:
print("func2 locked")
while stack:
await cond.wait()
assert cond.locked()
assert len(stack) == 0
# do something
await loop.sleep(2.5)
stack.append(random.random())
cond.notify()
print("func2 release")
loop.run_coroutine_func(func2)
loop.run_coroutine_func(func1)
loop.loop_forever()