-
Notifications
You must be signed in to change notification settings - Fork 2
/
tick.py
48 lines (35 loc) · 959 Bytes
/
tick.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
import asyncio
import time
import uuid
from typing import Tuple
from dependable import Depends, dependant
def get_id() -> uuid.UUID:
return uuid.uuid4()
async def get_remote_id() -> uuid.UUID:
await asyncio.sleep(1)
return uuid.uuid4()
def cpu_bound() -> None:
time.sleep(1)
return time.time()
@dependant
async def tick(
*,
i: int,
local_id: uuid.UUID = Depends(get_id),
remote_id: uuid.UUID = Depends(get_remote_id),
cpu_time: int = Depends(cpu_bound),
) -> Tuple[int, uuid.UUID, uuid.UUID, int]:
return (i, local_id, remote_id, cpu_time)
async def main() -> None:
ticks = set()
start = time.time()
print(f"start {start}")
for i in range(5):
t = asyncio.create_task(tick(i=i))
ticks.add(t)
done, pending = await asyncio.wait(ticks)
for d in done:
print(d.result())
end = time.time()
print(f"end {end}, duration {end-start}")
asyncio.run(main())