From 7b9b3e2d47a4ba5243776ba042994977f3d13e02 Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Thu, 3 Sep 2026 10:19:28 +0200 Subject: [PATCH] more experiments with fastapi and async / await --- pyproject.toml | 2 +- scopes/api/main.py | 2 +- scopes/core/actor.py | 44 ++++++++++++++++++++++++++------------ scopes/org/user.py | 3 +++ scopes/storage/tracking.py | 2 +- 5 files changed, 36 insertions(+), 17 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 797aa0c..ff5b794 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ app = [ "zope.traversing", ] auth = ["pyjwt[crypto]", "cryptography", "requests"] -fastapi = ["fastapi[standard]"] +api = ["fastapi[standard]"] test = ["zope.testrunner"] #test = ["pytest"] diff --git a/scopes/api/main.py b/scopes/api/main.py index 995f986..c9cefb7 100644 --- a/scopes/api/main.py +++ b/scopes/api/main.py @@ -1,4 +1,4 @@ -# package scopes.api.main +# scopes.api.main from fastapi import FastAPI diff --git a/scopes/core/actor.py b/scopes/core/actor.py index 87fa195..49fb885 100644 --- a/scopes/core/actor.py +++ b/scopes/core/actor.py @@ -1,32 +1,48 @@ -""" package scopes.core.actor """ +# scopes.core.actor import asyncio -quit = object() +QUIT = object() async def loop(q, bhv): while True: msg = await q.get() - if msg == quit: + if msg == QUIT: break - bhv(msg) + bhv = await bhv(msg) or bhv -def create(q, bhv): - return asyncio.create_task(loop(q, bhv)) +def create(bhv): + q = asyncio.Queue() + task = asyncio.create_task(loop(q, bhv)) + return q, task -# some simple behaviors +# interactive tests + +async def handle_basic(msg): + match msg[0]: + case "show": show(msg) + case "switch": return handle_alt + case _: show(msg) + +async def handle_alt(msg): + await asyncio.sleep(0.01) + match msg[0]: + case "show": print('no way') + case _: show(msg) 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 + q, task = create(handle_basic) + await q.put(["Hello World!"]) + await q.put(["switch"]) + await q.put(["show", "nope"]) + await q.put(["Hello World!"]) + await q.put(QUIT) + #await task + await asyncio.wait([task]) + #await asyncio.sleep(10) def run(): asyncio.run(main()) diff --git a/scopes/org/user.py b/scopes/org/user.py index 4c166c2..629dc5f 100644 --- a/scopes/org/user.py +++ b/scopes/org/user.py @@ -2,6 +2,9 @@ """Basic user account (principal) definitions + access to identity provider.""" +# should be moved to scopes.web.auth +# scopes.org: + person + from dataclasses import dataclass, field from typing import List, Optional diff --git a/scopes/storage/tracking.py b/scopes/storage/tracking.py index b1f8644..631dfb6 100644 --- a/scopes/storage/tracking.py +++ b/scopes/storage/tracking.py @@ -85,7 +85,7 @@ class Track: @registerContainerClass -class Container(object): +class Container: itemFactory = Track tableName = 'tracks'