some experiments with async / await: set up a simple actor framework
This commit is contained in:
parent
2b69416c44
commit
db1f78f4e6
2 changed files with 34 additions and 0 deletions
1
scopes/core/__init__.py
Normal file
1
scopes/core/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
"""package scopes.core"""
|
||||||
33
scopes/core/actor.py
Normal file
33
scopes/core/actor.py
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
""" 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())
|
||||||
|
|
||||||
Loading…
Add table
Reference in a new issue