Step toward linked tasks
This commit is contained in:
parent
3e538df711
commit
f942b6aefb
27
chat.py
27
chat.py
|
@ -1,7 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
import random
|
import random
|
||||||
import threading
|
|
||||||
import syndicate
|
import syndicate
|
||||||
from syndicate import patterns as P, actor
|
from syndicate import patterns as P, actor
|
||||||
from syndicate.schema import simpleChatProtocol, gatekeeper, sturdy, dataspace
|
from syndicate.schema import simpleChatProtocol, gatekeeper, sturdy, dataspace
|
||||||
|
@ -25,29 +24,35 @@ def print(*items):
|
||||||
_print(*items)
|
_print(*items)
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
def on_presence(turn, who):
|
|
||||||
print('%s joined' % (who,))
|
|
||||||
return lambda turn: print('%s left' % (who,))
|
|
||||||
|
|
||||||
def main_facet(turn, root_facet, ds):
|
def main_facet(turn, root_facet, ds):
|
||||||
print('main_facet', ds)
|
print('main_facet', ds)
|
||||||
f = turn._facet
|
f = turn._facet
|
||||||
turn.publish(ds, Present(me))
|
turn.publish(ds, Present(me))
|
||||||
|
|
||||||
|
def on_presence(turn, who):
|
||||||
|
print('%s joined' % (who,))
|
||||||
|
return lambda turn: print('%s left' % (who,))
|
||||||
turn.publish(ds, dataspace.Observe(P.rec('Present', P.CAPTURE),
|
turn.publish(ds, dataspace.Observe(P.rec('Present', P.CAPTURE),
|
||||||
During(turn, on_add = on_presence).ref))
|
During(turn, on_add = on_presence).ref))
|
||||||
turn.publish(ds, dataspace.Observe(P.rec('Says', P.CAPTURE, P.CAPTURE), During(
|
|
||||||
turn,
|
def on_says(turn, who, what):
|
||||||
on_msg = lambda turn, who, what: print('%s says %r' % (who, what))).ref))
|
print('%s says %r' % (who, what))
|
||||||
|
turn.publish(ds, dataspace.Observe(P.rec('Says', P.CAPTURE, P.CAPTURE),
|
||||||
|
During(turn, on_msg = on_says).ref))
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
def accept_input():
|
async def accept_input():
|
||||||
|
reader = asyncio.StreamReader()
|
||||||
|
print(await loop.connect_read_pipe(lambda: asyncio.StreamReaderProtocol(reader), sys.stdin))
|
||||||
while True:
|
while True:
|
||||||
line = sys.stdin.readline()
|
line = await reader.readline()
|
||||||
|
line = line.decode('utf-8')
|
||||||
if not line:
|
if not line:
|
||||||
actor.Turn.external(loop, f, lambda turn: turn.stop(root_facet))
|
actor.Turn.external(loop, f, lambda turn: turn.stop(root_facet))
|
||||||
break
|
break
|
||||||
actor.Turn.external(loop, f, lambda turn: turn.send(ds, Says(me, line.strip())))
|
actor.Turn.external(loop, f, lambda turn: turn.send(ds, Says(me, line.strip())))
|
||||||
threading.Thread(target=accept_input, daemon=True).start()
|
input_task = loop.create_task(accept_input())
|
||||||
|
turn._facet.on_stop(lambda turn: input_task.cancel())
|
||||||
|
|
||||||
def main(turn):
|
def main(turn):
|
||||||
root_facet = turn._facet
|
root_facet = turn._facet
|
||||||
|
|
Loading…
Reference in New Issue