|
15 | 15 | import asyncio |
16 | 16 | import random |
17 | 17 | import time |
| 18 | +from contextlib import ExitStack |
18 | 19 |
|
19 | 20 | from async_utils._simple_lock import AsyncLock # noqa: PLC2701 |
20 | 21 | from async_utils.bg_loop import threaded_loop |
21 | 22 |
|
| 23 | +min_res = time.get_clock_info("monotonic").resolution |
| 24 | + |
| 25 | +start = time.monotonic_ns() |
| 26 | + |
22 | 27 |
|
23 | 28 | async def check(lock: AsyncLock): |
24 | 29 | async with lock: |
25 | | - v = random.random() |
26 | | - s = time.monotonic() |
| 30 | + v = max(random.random() / 1000, min_res) |
| 31 | + s = time.monotonic_ns() |
27 | 32 | 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) |
30 | 36 |
|
31 | 37 |
|
32 | 38 | async def amain(): |
33 | 39 | 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)} |
41 | 47 | await asyncio.gather(*tsks) |
42 | 48 |
|
43 | 49 |
|
|
0 commit comments