From 87a6165430062104fe1dcf42040830af629b62c5 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sat, 12 Jan 2019 14:31:17 -0500 Subject: [PATCH] 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 --- tractor/__init__.py | 2 +- tractor/_ipc.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/tractor/__init__.py b/tractor/__init__.py index d8d6489..5ffcfd5 100644 --- a/tractor/__init__.py +++ b/tractor/__init__.py @@ -11,7 +11,7 @@ import trio # type: ignore from trio import MultiError 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 ( Actor, _start_actor, Arbiter, get_arbiter, find_actor, wait_for_actor ) diff --git a/tractor/_ipc.py b/tractor/_ipc.py index f7bebbe..41b8061 100644 --- a/tractor/_ipc.py +++ b/tractor/_ipc.py @@ -205,6 +205,28 @@ class Channel: 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 async def _connect_chan( host: str, port: int