more experiments with fastapi and async / await

This commit is contained in:
Helmut Merz 2026-09-03 10:19:28 +02:00
parent 778324ad33
commit 7b9b3e2d47
5 changed files with 36 additions and 17 deletions

View file

@ -30,7 +30,7 @@ app = [
"zope.traversing",
]
auth = ["pyjwt[crypto]", "cryptography", "requests"]
fastapi = ["fastapi[standard]"]
api = ["fastapi[standard]"]
test = ["zope.testrunner"]
#test = ["pytest"]

View file

@ -1,4 +1,4 @@
# package scopes.api.main
# scopes.api.main
from fastapi import FastAPI

View file

@ -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())

View file

@ -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

View file

@ -85,7 +85,7 @@ class Track:
@registerContainerClass
class Container(object):
class Container:
itemFactory = Track
tableName = 'tracks'