Skip to content

Commit d10b290

Browse files
authored
Merge branch 'main' into type-deterministiceventloop
2 parents 1bdf54f + 3de02fb commit d10b290

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

cadence/_internal/workflow/deterministic_event_loop.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ def __init__(self) -> None:
2828
self._stopping: bool = False
2929
self._closed: bool = False
3030

31+
def run_until_yield(self):
32+
"""Run until stop() is called."""
33+
self._run_forever_setup()
34+
try:
35+
while self._ready:
36+
self._run_once()
37+
finally:
38+
self._run_forever_cleanup()
39+
40+
# Event Loop APIs
3141
def call_soon(
3242
self,
3343
callback: Callable[[Unpack[_Ts]], object],

cadence/_internal/workflow/workflow_engine.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,7 @@ def _execute_workflow_once(
225225
self._workflow_instance.run(workflow_input)
226226
)
227227

228-
# signal the loop to stop after the first run
229-
self._loop.stop()
230-
# this starts the loop and runs once then stops with cleanup
231-
self._loop.run_forever()
228+
self._loop.run_until_yield()
232229

233230
except Exception as e:
234231
logger.error(

tests/cadence/_internal/workflow/test_deterministic_event_loop.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,17 @@ def test_create_task(self):
7676
size = 10000
7777
results = self.loop.run_until_complete(coro_await_task(size))
7878
assert results == list(range(size))
79+
80+
def test_run_once(self):
81+
# run once won't clear the read queue
82+
self.loop.create_task(coro_await_task(10))
83+
self.loop.stop()
84+
self.loop.run_forever()
85+
assert len(self.loop._ready) == 10
86+
87+
def test_run_until_yield(self):
88+
# run until yield will clear the read queue
89+
task = self.loop.create_task(coro_await_task(3))
90+
self.loop.run_until_yield()
91+
assert len(self.loop._ready) == 0
92+
assert task.done() is True

0 commit comments

Comments
 (0)