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", "zope.traversing",
] ]
auth = ["pyjwt[crypto]", "cryptography", "requests"] auth = ["pyjwt[crypto]", "cryptography", "requests"]
fastapi = ["fastapi[standard]"] api = ["fastapi[standard]"]
test = ["zope.testrunner"] test = ["zope.testrunner"]
#test = ["pytest"] #test = ["pytest"]

View file

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

View file

@ -1,32 +1,48 @@
""" package scopes.core.actor """ # scopes.core.actor
import asyncio import asyncio
quit = object() QUIT = object()
async def loop(q, bhv): async def loop(q, bhv):
while True: while True:
msg = await q.get() msg = await q.get()
if msg == quit: if msg == QUIT:
break break
bhv(msg) bhv = await bhv(msg) or bhv
def create(q, bhv): def create(bhv):
return asyncio.create_task(loop(q, 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): def show(msg):
print(msg) print(msg)
# main
async def main(): async def main():
q = asyncio.Queue() q, task = create(handle_basic)
task = create(q, show) await q.put(["Hello World!"])
await q.put("Hello World!") await q.put(["switch"])
await q.put(quit) await q.put(["show", "nope"])
await task await q.put(["Hello World!"])
await q.put(QUIT)
#await task
await asyncio.wait([task])
#await asyncio.sleep(10)
def run(): def run():
asyncio.run(main()) asyncio.run(main())

View file

@ -2,6 +2,9 @@
"""Basic user account (principal) definitions + access to identity provider.""" """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 dataclasses import dataclass, field
from typing import List, Optional from typing import List, Optional

View file

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