core.actor: try_send()
This commit is contained in:
parent
c78e1ac067
commit
f0de1a3e7a
1 changed files with 13 additions and 3 deletions
|
|
@ -4,9 +4,11 @@ import asyncio
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Awaitable, Callable
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from scopes.util import keywords as k
|
from scopes.util import keywords as k, Symbol
|
||||||
|
k('full quit')
|
||||||
|
|
||||||
type Behavior = Callable[[Any], Awaitable[Behavior | None]]
|
type Behavior = Callable[[Any], Awaitable[Behavior | None]]
|
||||||
|
type Mailbox = asyncio.Queue
|
||||||
|
|
||||||
async def loop(q: asyncio.Queue, bhv: Behavior) -> None:
|
async def loop(q: asyncio.Queue, bhv: Behavior) -> None:
|
||||||
while True:
|
while True:
|
||||||
|
|
@ -14,12 +16,20 @@ async def loop(q: asyncio.Queue, bhv: Behavior) -> None:
|
||||||
if msg is k.quit:
|
if msg is k.quit:
|
||||||
break
|
break
|
||||||
bhv = await bhv(msg) or bhv
|
bhv = await bhv(msg) or bhv
|
||||||
|
q.task_done()
|
||||||
|
|
||||||
def create(bhv: Behavior, group: Optional[asyncio.TaskGroup] = None) -> asyncio.Queue:
|
def create(bhv: Behavior, maxsize: int = 0,
|
||||||
q: asyncio.Queue = asyncio.Queue()
|
group: Optional[asyncio.TaskGroup] = None) -> asyncio.Queue:
|
||||||
|
q: asyncio.Queue = asyncio.Queue(maxsize)
|
||||||
if group is None:
|
if group is None:
|
||||||
asyncio.create_task(loop(q, bhv))
|
asyncio.create_task(loop(q, bhv))
|
||||||
else:
|
else:
|
||||||
group.create_task(loop(q, bhv))
|
group.create_task(loop(q, bhv))
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
async def try_send(rcvr: Mailbox, msg: Any) -> Symbol | None:
|
||||||
|
if rcvr.full():
|
||||||
|
return k.full
|
||||||
|
await rcvr.put(msg)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue