Add a `Context` type for task/protocol RPC state

This is loosely based off the nanomsg concept found here:
https://nanomsg.github.io/nng/man/v1.1.0/nng_ctx.5
contexts
Tyler Goodlet 2019-01-12 14:31:17 -05:00
parent a9932e6c01
commit 87a6165430
2 changed files with 23 additions and 1 deletions

View File

@ -11,7 +11,7 @@ import trio # type: ignore
from trio import MultiError from trio import MultiError
from .log import get_console_log, get_logger, get_loglevel from .log import get_console_log, get_logger, get_loglevel
from ._ipc import _connect_chan, Channel from ._ipc import _connect_chan, Channel, Context
from ._actor import ( from ._actor import (
Actor, _start_actor, Arbiter, get_arbiter, find_actor, wait_for_actor Actor, _start_actor, Arbiter, get_arbiter, find_actor, wait_for_actor
) )

View File

@ -205,6 +205,28 @@ class Channel:
return self.squeue.connected() if self.squeue else False return self.squeue.connected() if self.squeue else False
class Context:
"""An IAC (inter-actor communication) context.
Allows maintaining task or protocol specific state between communicating
actors. A unique context is created on the receiving end for every request
to a remote actor.
"""
def __init__(
self,
channel: Channel,
command_id: str,
):
self.chan: Channel = channel
self.cid: str = command_id
async def send_yield(self, data):
await self.chan.send({'yield': data, 'cid': self.cid})
async def send_stop(self):
await self.chan.send({'stop': True, 'cid': self.cid})
@asynccontextmanager @asynccontextmanager
async def _connect_chan( async def _connect_chan(
host: str, port: int host: str, port: int