-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
44 lines (32 loc) · 990 Bytes
/
test.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
import asyncio
import etcd3_asyncio as etcd3
async def main():
async for e in etcd3.Watch(b'/devices/'):
print(e)
async def locker(n):
lock = etcd3.Lock('a')
async with lock:
print(n, 'locked')
print(n, 'released')
async def conditioner(n):
cond = etcd3.Condition('a')
async with cond:
while True:
get = etcd3.Get('a', default=0)
r = await cond.wait(get)
if int(r[0]) == n:
await etcd3.Put('a', n+1)
cond.notify_all()
print(n, '->', n+1)
break
async def notifier():
cond = etcd3.Condition('a')
await asyncio.sleep(5)
cond.notify_all()
asyncio.get_event_loop().create_task(main())
for n in range(0):
asyncio.get_event_loop().create_task(locker(n))
for n in range(0):
asyncio.get_event_loop().create_task(conditioner(99-n))
asyncio.get_event_loop().create_task(notifier())
asyncio.get_event_loop().run_forever()