From 2e208349d2e6f8f3d7fbbe853413549da9df0ce0 Mon Sep 17 00:00:00 2001 From: bd Date: Sat, 19 Jul 2025 23:45:36 -0600 Subject: Relicensed as Apache2.0 --- t/manual/random_wait.py | 22 ++++++++++++++++++++++ t/random_wait.py | 22 ---------------------- t/test_functionality.py | 41 ++++++++++++++++++++++++++++++++++++++++- t/threads.py | 23 ----------------------- 4 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 t/manual/random_wait.py delete mode 100644 t/random_wait.py delete mode 100644 t/threads.py (limited to 't') diff --git a/t/manual/random_wait.py b/t/manual/random_wait.py new file mode 100644 index 0000000..2cfc290 --- /dev/null +++ b/t/manual/random_wait.py @@ -0,0 +1,22 @@ +# SuperFastPython.com +# example of waiting for all tasks to complete +from random import random +import asyncio + +total = 0 + + +async def task_coro(arg): + value = random() + total += value + await asyncio.sleep(value) + print(f'>{arg} done in {value}') + + +async def main(): + tasks = [asyncio.create_task(task_coro(i)) for i in range(10)] + done, pending = await asyncio.wait(tasks) + print(f'All done. Total waiting time: {total}') + + +asyncio.run(main()) diff --git a/t/random_wait.py b/t/random_wait.py deleted file mode 100644 index 2cfc290..0000000 --- a/t/random_wait.py +++ /dev/null @@ -1,22 +0,0 @@ -# SuperFastPython.com -# example of waiting for all tasks to complete -from random import random -import asyncio - -total = 0 - - -async def task_coro(arg): - value = random() - total += value - await asyncio.sleep(value) - print(f'>{arg} done in {value}') - - -async def main(): - tasks = [asyncio.create_task(task_coro(i)) for i in range(10)] - done, pending = await asyncio.wait(tasks) - print(f'All done. Total waiting time: {total}') - - -asyncio.run(main()) diff --git a/t/test_functionality.py b/t/test_functionality.py index 12bf775..6daf2d3 100644 --- a/t/test_functionality.py +++ b/t/test_functionality.py @@ -1,6 +1,6 @@ import utils -import aiohttp import asyncio +import threading class BasicUsage(utils.AergiaUnitTestCase): @@ -54,7 +54,26 @@ class BasicUsage(utils.AergiaUnitTestCase): self.assertFuncContains('b', [self.expected_samples(delay * 3)], samples) + # TODO samples from gather all execution time, should we trace this?? + self.assertFuncContains('a', [self.expected_samples(delay)], samples) + + def test_alter_thread_task(self): + delay = 0.2 + + async def b(): await asyncio.sleep(delay) + async def a(): await asyncio.gather(b(), b(), b()) + def c(): asyncio.run(a()) + self.Aergia.start() + x = threading.Thread(target=c) + x.start() + x.join() + self.Aergia.stop() + + samples = self.Aergia.get_samples() + self.assertFuncContains('c', [], samples) + self.assertFuncContains('b', [self.expected_samples(delay * 3)], + samples) # TODO samples from gather all execution time, should we trace this?? self.assertFuncContains('a', [self.expected_samples(delay)], samples) @@ -71,3 +90,23 @@ class BasicUsage(utils.AergiaUnitTestCase): samples = self.Aergia.get_samples() self.assertFuncContains('a', [self.expected_samples(delay)], samples) + + def test_async_gen_and_comp(self): + delay = 0.2 + num_times = 10 + + async def b(): + for i in range(num_times): + await asyncio.sleep(delay) + yield i + + async def a(): + return [r async for r in b()] + + self.Aergia.start() + asyncio.run(a()) + self.Aergia.stop() + + samples = self.Aergia.get_samples() + self.assertFuncContains('b', [self.expected_samples(delay * num_times)], + samples) diff --git a/t/threads.py b/t/threads.py deleted file mode 100644 index fa9c7ac..0000000 --- a/t/threads.py +++ /dev/null @@ -1,23 +0,0 @@ -import asyncio -import threading - - - -async def count(): - print("it's a secret!") - await asyncio.sleep(3) - - -async def main(): - await asyncio.gather(count(), count(), count()) - print("done") - - -def thread_func(): - asyncio.run(main()) - - -if __name__ == "__main__": - x = threading.Thread(target=thread_func) - x.start() - x.join() -- cgit v1.2.3