Skip to content

Commit a59fd70

Browse files
committed
make script more useful
1 parent 83fc027 commit a59fd70

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

_misc/_l.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,35 @@
1515
import asyncio
1616
import random
1717
import time
18+
from contextlib import ExitStack
1819

1920
from async_utils._simple_lock import AsyncLock # noqa: PLC2701
2021
from async_utils.bg_loop import threaded_loop
2122

23+
min_res = time.get_clock_info("monotonic").resolution
24+
25+
start = time.monotonic_ns()
26+
2227

2328
async def check(lock: AsyncLock):
2429
async with lock:
25-
v = random.random()
26-
s = time.monotonic()
30+
v = max(random.random() / 1000, min_res)
31+
s = time.monotonic_ns()
2732
await asyncio.sleep(v)
28-
e = time.monotonic()
29-
print(s, v, e, flush=True) # noqa: T201
33+
e = time.monotonic_ns()
34+
print(s - start, e - start, flush=True) # noqa: T201
35+
await asyncio.sleep(min_res)
3036

3137

3238
async def amain():
3339
lock = AsyncLock()
34-
with (
35-
threaded_loop(use_eager_task_factory=False) as tl1,
36-
threaded_loop(use_eager_task_factory=False) as tl2,
37-
threaded_loop() as tl3,
38-
threaded_loop() as tl4,
39-
):
40-
tsks = {loop.run(check(lock)) for loop in (tl1, tl2, tl3, tl4) for _ in range(10)}
40+
with ExitStack() as ex:
41+
loops = [
42+
ex.enter_context(threaded_loop(use_eager_task_factory=x))
43+
for _ in range(10)
44+
for x in (True, False)
45+
]
46+
tsks = {loop.run(check(lock)) for loop in loops for _ in range(10)}
4147
await asyncio.gather(*tsks)
4248

4349

0 commit comments

Comments
 (0)