From db1f78f4e6863dcd8198d169001503fb30733665 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sat, 4 Jul 2026 21:23:13 +0200 Subject: [PATCH] some experiments with async / await: set up a simple actor framework --- scopes/core/__init__.py | 1 + scopes/core/actor.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 scopes/core/__init__.py create mode 100644 scopes/core/actor.py diff --git a/scopes/core/__init__.py b/scopes/core/__init__.py new file mode 100644 index 0000000..1d165e4 --- /dev/null +++ b/scopes/core/__init__.py @@ -0,0 +1 @@ +"""package scopes.core""" diff --git a/scopes/core/actor.py b/scopes/core/actor.py new file mode 100644 index 0000000..87fa195 --- /dev/null +++ b/scopes/core/actor.py @@ -0,0 +1,33 @@ +""" package scopes.core.actor """ + +import asyncio + +quit = object() + +async def loop(q, bhv): + while True: + msg = await q.get() + if msg == quit: + break + bhv(msg) + +def create(q, bhv): + return asyncio.create_task(loop(q, bhv)) + +# some simple behaviors + +def show(msg): + print(msg) + +# main + +async def main(): + q = asyncio.Queue() + task = create(q, show) + await q.put("Hello World!") + await q.put(quit) + await task + +def run(): + asyncio.run(main()) +