diff options
| author | bd <bdunahu@operationnull.com> | 2025-12-14 03:19:54 -0500 |
|---|---|---|
| committer | bd <bdunahu@operationnull.com> | 2025-12-14 03:19:54 -0500 |
| commit | 8a856eb6d4b373f4dfe87a16c3fef265ab8fd69a (patch) | |
| tree | d281299aa2fa7e1c8de2ed4e0efec6ced14d7d0e | |
| parent | 412c8225046b7549e0b02425f5140f2a33abac8a (diff) | |
ex: add block_longtime example
| -rw-r--r-- | ex/block_longtime.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/ex/block_longtime.py b/ex/block_longtime.py new file mode 100644 index 0000000..aa1ae13 --- /dev/null +++ b/ex/block_longtime.py @@ -0,0 +1,30 @@ +import asyncio +import time +import sys +import os +sys.path.append(os.path.expanduser('~/nemesis/nemesis')) +from causal_event_loop import causal_loop_factory + + +async def burn_cpu(sec): + t0 = time.perf_counter() + elapsed = 0 + while (elapsed < sec): + for _ in range(1000): + pass + elapsed = time.perf_counter() - t0 + +async def sleep_task(duration): + await asyncio.sleep(duration) + +async def main(): + total_duration = 10 + sleep_durations = [1.0, 0.7, 0.7, 0.4] + + for _ in range(total_duration): + tasks = [asyncio.create_task(sleep_task(duration)) for duration in sleep_durations] + burn_task = asyncio.create_task(burn_cpu(1)) + + await asyncio.gather(burn_task, *tasks) + +asyncio.run(main(), loop_factory=causal_loop_factory) |
