py-scopes/scopes/core/actor.py

33 lines
501 B
Python

""" package scopes.core.actor """
import asyncio
quit = object()
async def loop(q, bhv):
while True:
msg = await q.get()
if msg == quit:
break
bhv(msg)
def create(q, bhv):
return asyncio.create_task(loop(q, bhv))
# some simple behaviors
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
def run():
asyncio.run(main())