From ed48076e6761ff07be8318e0d4901cd06722d2ed Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Sun, 18 Oct 2026 16:35:59 +0200 Subject: [PATCH] core: setting up env neuron basically working --- scopes/core/core.py | 7 +++++++ scopes/core/environ.py | 3 ++- tests/test_core.py | 18 ++++++++---------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/scopes/core/core.py b/scopes/core/core.py index a3c312d..d27ad44 100644 --- a/scopes/core/core.py +++ b/scopes/core/core.py @@ -8,6 +8,7 @@ from collections.abc import Awaitable, Callable, Coroutine from dataclasses import dataclass, field from typing import Any +from scopes.core import actor from scopes.core.message import Message from scopes.util import keywords as k, Symbol @@ -45,3 +46,9 @@ def create_scope(env: Mailbox, cat=(k.csys, k.c00), **kw) -> Scope: scope = Scope(environ=env, categ=cat, **kw) return scope.set_proc() +def neuron(scope: Scope) -> Mailbox: + async def process(msg: Message) -> actor.Behavior | None: + nonlocal scope + scope = await scope.proc(scope, msg) + return None if scope is None else process + return actor.create(process) diff --git a/scopes/core/environ.py b/scopes/core/environ.py index 33c6222..9164896 100644 --- a/scopes/core/environ.py +++ b/scopes/core/environ.py @@ -10,5 +10,6 @@ async def proc_env(scope, msg): await scope.environ.put(msg) def create(meta_env): - return core.create_scope(meta_env, (k.env, k.c00), program=lambda x: proc_env) + scope = core.create_scope(meta_env, (k.env, k.c00), program=lambda x: proc_env) + return core.neuron(scope) diff --git a/tests/test_core.py b/tests/test_core.py index 165ac6a..48920bd 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -6,7 +6,8 @@ import asyncio import pytest from scopes import core -from scopes.core import actor, environ, message +from scopes.core import actor, environ +from scopes.core.message import Message from scopes.util import keywords as k collector = [] @@ -32,11 +33,8 @@ async def test_actor(): async with asyncio.TaskGroup() as group: q1 = actor.create(handle_basic, group) q2 = actor.create(handle_basic, group) - await q1.put(message.Message(k.test, k.hello)) - await q1.put(k.show) - await q1.put(k.switch) - await q1.put(k.show) - await q1.put("actor 1: Goodbye") + msgs = [Message(k.test, k.hello), k.show, k.switch, k.show, "actor 1: Goodbye"] + await asyncio.gather(*[q1.put(m) for m in msgs]) await q2.put("actor 2") await q2.put(k.show) await q1.put(k.quit) @@ -47,7 +45,7 @@ async def test_actor(): def test_message(): - msg = message.Message(k.test, k.data, 'person', '4711', + msg = Message(k.test, k.data, 'person', '4711', data=dict(firstName='Bob', lastName='Miller')) assert msg.head['class'] is k.person assert msg.addr() == (k.test, k.person, '4711') @@ -66,8 +64,8 @@ async def meta_env(coll): @pytest.mark.anyio async def test_core(meta_env, coll): - env_scope = environ.create(meta_env) - assert env_scope.value == 0 - #print(meta_env) + env = environ.create(meta_env) + await env.put(Message(k.env, k.hello)) await asyncio.sleep(0.01) + print(coll)