- commit
- 8ed7defd595a8e895c90830093d01959b19a03cc
- parent
- d39fb2b7a1d88a6447fe62d7206fea21e836dabe
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2026-02-26 18:37
async/await syntax This is just an abstraction on top of generators. Our function is now called a *coroutine*.
Diffstat
| M | tests.py | 12 | ++++++------ |
| M | xiio.py | 16 | ++++++++++++++-- |
2 files changed, 20 insertions, 8 deletions
diff --git a/tests.py b/tests.py
@@ -19,8 +19,8 @@ class XiioTestCase(unittest.TestCase): 19 19 20 20 class TestRun(XiioTestCase): 21 21 def test_sleep(self):22 -1 def foo():23 -1 yield 0.1-1 22 async def foo(): -1 23 await xiio.Timeout(0.1) 24 24 return 'Hello World' 25 25 26 26 with self.assert_duration(0.1): @@ -30,9 +30,9 @@ class TestRun(XiioTestCase): 30 30 def test_runs_cleanup_on_error_while_paused(self): 31 31 stack = [] 32 3233 -1 def foo():-1 33 async def foo(): 34 34 try:35 -1 yield 0.1-1 35 await xiio.Timeout(0.1) 36 36 finally: 37 37 stack.append(1) 38 38 @@ -42,11 +42,11 @@ class TestRun(XiioTestCase): 42 42 self.assertEqual(stack, [1]) 43 43 44 44 def test_waits_for_cleanup(self):45 -1 def foo():-1 45 async def foo(): 46 46 try: 47 47 raise ValueError 48 48 finally:49 -1 yield 0.1-1 49 await xiio.Timeout(0.1) 50 50 51 51 with self.assertRaises(ValueError): 52 52 with self.assert_duration(0.1):
diff --git a/xiio.py b/xiio.py
@@ -1,12 +1,24 @@ 1 1 import time 2 2 3 34 -1 def run(gen):-1 4 class Timeout: -1 5 def __init__(self, seconds): -1 6 self.seconds = seconds -1 7 -1 8 def __await__(self): -1 9 yield self -1 10 -1 11 def wait(self): -1 12 time.sleep(self.seconds) -1 13 -1 14 -1 15 def run(coro): -1 16 gen = coro.__await__() 5 17 try: 6 18 timeout = next(gen) 7 19 while True: 8 20 try:9 -1 time.sleep(timeout)-1 21 timeout.wait() 10 22 except BaseException as e: 11 23 timeout = gen.throw(e) 12 24 else: