2021-12-13 18:08:32 +00:00
|
|
|
# tractor: structured concurrent "actors".
|
|
|
|
# Copyright 2018-eternity Tyler Goodlet.
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-05-07 15:52:08 +00:00
|
|
|
"""
|
|
|
|
Message stream types and APIs.
|
|
|
|
|
|
|
|
"""
|
2021-08-15 21:42:10 +00:00
|
|
|
from __future__ import annotations
|
2019-03-29 23:10:32 +00:00
|
|
|
import inspect
|
2021-10-05 23:25:28 +00:00
|
|
|
from contextlib import asynccontextmanager
|
2019-03-26 01:36:13 +00:00
|
|
|
from dataclasses import dataclass
|
2021-05-07 15:52:08 +00:00
|
|
|
from typing import (
|
2022-09-15 20:56:50 +00:00
|
|
|
Any,
|
|
|
|
Optional,
|
|
|
|
Callable,
|
|
|
|
AsyncGenerator,
|
2021-08-31 17:06:17 +00:00
|
|
|
AsyncIterator
|
2021-05-07 15:52:08 +00:00
|
|
|
)
|
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
import warnings
|
2019-03-26 01:36:13 +00:00
|
|
|
|
|
|
|
import trio
|
|
|
|
|
|
|
|
from ._ipc import Channel
|
2021-06-30 17:47:38 +00:00
|
|
|
from ._exceptions import unpack_error, ContextCancelled
|
2021-05-01 19:10:03 +00:00
|
|
|
from ._state import current_actor
|
2021-04-27 14:03:00 +00:00
|
|
|
from .log import get_logger
|
2021-10-04 15:22:10 +00:00
|
|
|
from .trionics import broadcast_receiver, BroadcastReceiver
|
2019-03-26 01:36:13 +00:00
|
|
|
|
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
log = get_logger(__name__)
|
2019-03-26 01:36:13 +00:00
|
|
|
|
|
|
|
|
2021-12-13 18:08:32 +00:00
|
|
|
# TODO: the list
|
|
|
|
# - generic typing like trio's receive channel but with msgspec
|
|
|
|
# messages? class ReceiveChannel(AsyncResource, Generic[ReceiveType]):
|
|
|
|
# - use __slots__ on ``Context``?
|
2021-04-27 14:03:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReceiveMsgStream(trio.abc.ReceiveChannel):
|
2021-12-03 19:27:04 +00:00
|
|
|
'''
|
|
|
|
A IPC message stream for receiving logically sequenced values over
|
|
|
|
an inter-actor ``Channel``. This is the type returned to a local
|
|
|
|
task which entered either ``Portal.open_stream_from()`` or
|
2021-09-02 12:24:13 +00:00
|
|
|
``Context.open_stream()``.
|
2021-04-27 14:03:00 +00:00
|
|
|
|
|
|
|
Termination rules:
|
2021-06-13 23:58:52 +00:00
|
|
|
|
2021-09-02 12:24:13 +00:00
|
|
|
- on cancellation the stream is **not** implicitly closed and the
|
|
|
|
surrounding ``Context`` is expected to handle how that cancel
|
|
|
|
is relayed to any task on the remote side.
|
|
|
|
- if the remote task signals the end of a stream the
|
|
|
|
``ReceiveChannel`` semantics dictate that a ``StopAsyncIteration``
|
|
|
|
to terminate the local ``async for``.
|
2021-04-27 14:03:00 +00:00
|
|
|
|
2021-09-02 12:24:13 +00:00
|
|
|
'''
|
2021-04-27 14:03:00 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2021-05-01 19:10:03 +00:00
|
|
|
ctx: 'Context', # typing: ignore # noqa
|
2021-08-16 16:47:49 +00:00
|
|
|
rx_chan: trio.MemoryReceiveChannel,
|
2021-08-19 16:36:05 +00:00
|
|
|
_broadcaster: Optional[BroadcastReceiver] = None,
|
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
) -> None:
|
|
|
|
self._ctx = ctx
|
|
|
|
self._rx_chan = rx_chan
|
2021-08-19 16:36:05 +00:00
|
|
|
self._broadcaster = _broadcaster
|
2021-04-27 14:03:00 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
# flag to denote end of stream
|
|
|
|
self._eoc: bool = False
|
2021-12-15 21:22:04 +00:00
|
|
|
self._closed: bool = False
|
2021-06-13 23:58:52 +00:00
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
# delegate directly to underlying mem channel
|
|
|
|
def receive_nowait(self):
|
2021-05-12 03:42:34 +00:00
|
|
|
msg = self._rx_chan.receive_nowait()
|
|
|
|
return msg['yield']
|
2021-04-27 14:03:00 +00:00
|
|
|
|
|
|
|
async def receive(self):
|
2021-09-02 12:24:13 +00:00
|
|
|
'''Async receive a single msg from the IPC transport, the next
|
|
|
|
in sequence for this stream.
|
|
|
|
|
|
|
|
'''
|
2021-06-14 20:34:44 +00:00
|
|
|
# see ``.aclose()`` for notes on the old behaviour prior to
|
|
|
|
# introducing this
|
2021-06-13 23:58:52 +00:00
|
|
|
if self._eoc:
|
|
|
|
raise trio.EndOfChannel
|
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
try:
|
|
|
|
msg = await self._rx_chan.receive()
|
|
|
|
return msg['yield']
|
|
|
|
|
2021-12-15 21:22:04 +00:00
|
|
|
except KeyError as err:
|
2021-04-27 14:03:00 +00:00
|
|
|
# internal error should never get here
|
|
|
|
assert msg.get('cid'), ("Received internal error at portal?")
|
|
|
|
|
|
|
|
# TODO: handle 2 cases with 3.10 match syntax
|
|
|
|
# - 'stop'
|
|
|
|
# - 'error'
|
|
|
|
# possibly just handle msg['stop'] here!
|
|
|
|
|
2021-12-15 21:22:04 +00:00
|
|
|
if msg.get('stop') or self._eoc:
|
2021-05-01 19:10:03 +00:00
|
|
|
log.debug(f"{self} was stopped at remote end")
|
2021-06-13 23:58:52 +00:00
|
|
|
|
2021-12-15 21:22:04 +00:00
|
|
|
# XXX: important to set so that a new ``.receive()``
|
|
|
|
# call (likely by another task using a broadcast receiver)
|
|
|
|
# doesn't accidentally pull the ``return`` message
|
|
|
|
# value out of the underlying feed mem chan!
|
|
|
|
self._eoc = True
|
|
|
|
|
2021-06-14 20:34:44 +00:00
|
|
|
# # when the send is closed we assume the stream has
|
|
|
|
# # terminated and signal this local iterator to stop
|
|
|
|
# await self.aclose()
|
2021-06-13 23:58:52 +00:00
|
|
|
|
|
|
|
# XXX: this causes ``ReceiveChannel.__anext__()`` to
|
2021-06-14 20:34:44 +00:00
|
|
|
# raise a ``StopAsyncIteration`` **and** in our catch
|
|
|
|
# block below it will trigger ``.aclose()``.
|
2021-12-15 21:22:04 +00:00
|
|
|
raise trio.EndOfChannel from err
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
# TODO: test that shows stream raising an expected error!!!
|
2021-05-01 19:10:03 +00:00
|
|
|
elif msg.get('error'):
|
2021-04-27 14:03:00 +00:00
|
|
|
# raise the error message
|
2021-05-01 19:10:03 +00:00
|
|
|
raise unpack_error(msg, self._ctx.chan)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise
|
2021-04-27 14:03:00 +00:00
|
|
|
|
2021-06-14 20:34:44 +00:00
|
|
|
except (
|
|
|
|
trio.ClosedResourceError, # by self._rx_chan
|
|
|
|
trio.EndOfChannel, # by self._rx_chan or `stop` msg from far end
|
|
|
|
):
|
|
|
|
# XXX: we close the stream on any of these error conditions:
|
|
|
|
|
|
|
|
# a ``ClosedResourceError`` indicates that the internal
|
|
|
|
# feeder memory receive channel was closed likely by the
|
|
|
|
# runtime after the associated transport-channel
|
|
|
|
# disconnected or broke.
|
|
|
|
|
|
|
|
# an ``EndOfChannel`` indicates either the internal recv
|
|
|
|
# memchan exhausted **or** we raisesd it just above after
|
|
|
|
# receiving a `stop` message from the far end of the stream.
|
|
|
|
|
|
|
|
# Previously this was triggered by calling ``.aclose()`` on
|
2021-04-27 14:03:00 +00:00
|
|
|
# the send side of the channel inside
|
2021-06-14 20:34:44 +00:00
|
|
|
# ``Actor._push_result()`` (should still be commented code
|
|
|
|
# there - which should eventually get removed), but now the
|
|
|
|
# 'stop' message handling has been put just above.
|
2021-04-27 14:03:00 +00:00
|
|
|
|
2021-06-14 20:34:44 +00:00
|
|
|
# TODO: Locally, we want to close this stream gracefully, by
|
2021-04-27 14:03:00 +00:00
|
|
|
# terminating any local consumers tasks deterministically.
|
2021-06-14 20:34:44 +00:00
|
|
|
# One we have broadcast support, we **don't** want to be
|
|
|
|
# closing this stream and not flushing a final value to
|
|
|
|
# remaining (clone) consumers who may not have been
|
|
|
|
# scheduled to receive it yet.
|
2021-04-27 14:03:00 +00:00
|
|
|
|
|
|
|
# when the send is closed we assume the stream has
|
|
|
|
# terminated and signal this local iterator to stop
|
|
|
|
await self.aclose()
|
2021-05-12 03:42:34 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
raise # propagate
|
2021-04-27 14:03:00 +00:00
|
|
|
|
|
|
|
async def aclose(self):
|
2021-12-15 21:22:04 +00:00
|
|
|
'''
|
|
|
|
Cancel associated remote actor task and local memory channel on
|
|
|
|
close.
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-12-15 21:22:04 +00:00
|
|
|
'''
|
2021-06-13 23:58:52 +00:00
|
|
|
# XXX: keep proper adherance to trio's `.aclose()` semantics:
|
2021-05-01 19:10:03 +00:00
|
|
|
# https://trio.readthedocs.io/en/stable/reference-io.html#trio.abc.AsyncResource.aclose
|
2021-04-27 14:03:00 +00:00
|
|
|
rx_chan = self._rx_chan
|
|
|
|
|
2021-06-30 17:47:38 +00:00
|
|
|
if rx_chan._closed:
|
2021-10-05 23:25:28 +00:00
|
|
|
log.cancel(f"{self} is already closed")
|
2021-06-13 23:58:52 +00:00
|
|
|
|
|
|
|
# this stream has already been closed so silently succeed as
|
|
|
|
# per ``trio.AsyncResource`` semantics.
|
|
|
|
# https://trio.readthedocs.io/en/stable/reference-io.html#trio.abc.AsyncResource.aclose
|
2021-04-27 14:03:00 +00:00
|
|
|
return
|
|
|
|
|
2021-12-15 22:19:04 +00:00
|
|
|
self._eoc = True
|
2021-12-15 21:22:04 +00:00
|
|
|
self._closed = True
|
2021-06-14 20:34:44 +00:00
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
# NOTE: this is super subtle IPC messaging stuff:
|
|
|
|
# Relay stop iteration to far end **iff** we're
|
|
|
|
# in bidirectional mode. If we're only streaming
|
|
|
|
# *from* one side then that side **won't** have an
|
|
|
|
# entry in `Actor._cids2qs` (maybe it should though?).
|
|
|
|
# So any `yield` or `stop` msgs sent from the caller side
|
|
|
|
# will cause key errors on the callee side since there is
|
|
|
|
# no entry for a local feeder mem chan since the callee task
|
|
|
|
# isn't expecting messages to be sent by the caller.
|
|
|
|
# Thus, we must check that this context DOES NOT
|
|
|
|
# have a portal reference to ensure this is indeed the callee
|
2021-06-14 20:34:44 +00:00
|
|
|
# side and can relay a 'stop'.
|
|
|
|
|
|
|
|
# In the bidirectional case, `Context.open_stream()` will create
|
|
|
|
# the `Actor._cids2qs` entry from a call to
|
2021-12-03 19:27:04 +00:00
|
|
|
# `Actor.get_context()` and will send the stop message in
|
2021-06-14 20:34:44 +00:00
|
|
|
# ``__aexit__()`` on teardown so it **does not** need to be
|
|
|
|
# called here.
|
2021-05-01 19:10:03 +00:00
|
|
|
if not self._ctx._portal:
|
2021-09-02 12:24:13 +00:00
|
|
|
# Only for 2 way streams can we can send stop from the
|
|
|
|
# caller side.
|
2021-06-13 23:58:52 +00:00
|
|
|
try:
|
2021-09-02 12:24:13 +00:00
|
|
|
# NOTE: if this call is cancelled we expect this end to
|
|
|
|
# handle as though the stop was never sent (though if it
|
|
|
|
# was it shouldn't matter since it's unlikely a user
|
|
|
|
# will try to re-use a stream after attemping to close
|
|
|
|
# it).
|
2021-12-06 00:19:53 +00:00
|
|
|
with trio.CancelScope(shield=True):
|
|
|
|
await self._ctx.send_stop()
|
2021-06-13 23:58:52 +00:00
|
|
|
|
2021-06-24 23:56:05 +00:00
|
|
|
except (
|
|
|
|
trio.BrokenResourceError,
|
|
|
|
trio.ClosedResourceError
|
|
|
|
):
|
2021-06-13 23:58:52 +00:00
|
|
|
# the underlying channel may already have been pulled
|
|
|
|
# in which case our stop message is meaningless since
|
|
|
|
# it can't traverse the transport.
|
|
|
|
log.debug(f'Channel for {self} was already closed')
|
|
|
|
|
2021-09-02 12:24:13 +00:00
|
|
|
# Do we close the local mem chan ``self._rx_chan`` ??!?
|
2021-06-13 23:58:52 +00:00
|
|
|
|
2021-09-02 12:24:13 +00:00
|
|
|
# NO, DEFINITELY NOT if we're a bi-dir ``MsgStream``!
|
2021-06-13 23:58:52 +00:00
|
|
|
# BECAUSE this same core-msg-loop mem recv-chan is used to deliver
|
|
|
|
# the potential final result from the surrounding inter-actor
|
|
|
|
# `Context` so we don't want to close it until that context has
|
|
|
|
# run to completion.
|
|
|
|
|
2021-06-14 20:34:44 +00:00
|
|
|
# XXX: Notes on old behaviour:
|
|
|
|
# await rx_chan.aclose()
|
2021-06-13 23:58:52 +00:00
|
|
|
|
2021-06-14 20:34:44 +00:00
|
|
|
# In the receive-only case, ``Portal.open_stream_from()`` used
|
|
|
|
# to rely on this call explicitly on teardown such that a new
|
|
|
|
# call to ``.receive()`` after ``rx_chan`` had been closed, would
|
|
|
|
# result in us raising a ``trio.EndOfChannel`` (since we
|
|
|
|
# remapped the ``trio.ClosedResourceError`). However, now if for some
|
|
|
|
# reason the stream's consumer code tries to manually receive a new
|
2021-06-13 23:58:52 +00:00
|
|
|
# value before ``.aclose()`` is called **but** the far end has
|
|
|
|
# stopped `.receive()` **must** raise ``trio.EndofChannel`` in
|
2021-06-14 20:34:44 +00:00
|
|
|
# order to avoid an infinite hang on ``.__anext__()``; this is
|
|
|
|
# why we added ``self._eoc`` to denote stream closure indepedent
|
|
|
|
# of ``rx_chan``.
|
|
|
|
|
|
|
|
# In theory we could still use this old method and close the
|
|
|
|
# underlying msg-loop mem chan as above and then **not** check
|
|
|
|
# for ``self._eoc`` in ``.receive()`` (if for some reason we
|
|
|
|
# think that check is a bottle neck - not likely) **but** then
|
|
|
|
# we would need to map the resulting
|
|
|
|
# ``trio.ClosedResourceError`` to a ``trio.EndOfChannel`` in
|
|
|
|
# ``.receive()`` (as it originally was before bi-dir streaming
|
|
|
|
# support) in order to trigger stream closure. The old behaviour
|
|
|
|
# is arguably more confusing since we lose detection of the
|
|
|
|
# runtime's closure of ``rx_chan`` in the case where we may
|
|
|
|
# still need to consume msgs that are "in transit" from the far
|
|
|
|
# end (eg. for ``Context.result()``).
|
2021-04-27 14:03:00 +00:00
|
|
|
|
2021-08-15 21:42:10 +00:00
|
|
|
@asynccontextmanager
|
|
|
|
async def subscribe(
|
|
|
|
self,
|
|
|
|
|
2021-08-16 16:47:49 +00:00
|
|
|
) -> AsyncIterator[BroadcastReceiver]:
|
2021-08-15 21:42:10 +00:00
|
|
|
'''Allocate and return a ``BroadcastReceiver`` which delegates
|
|
|
|
to this message stream.
|
|
|
|
|
|
|
|
This allows multiple local tasks to receive each their own copy
|
|
|
|
of this message stream.
|
|
|
|
|
|
|
|
This operation is indempotent and and mutates this stream's
|
|
|
|
receive machinery to copy and window-length-store each received
|
|
|
|
value from the far end via the internally created broudcast
|
|
|
|
receiver wrapper.
|
|
|
|
|
|
|
|
'''
|
2021-08-16 16:47:49 +00:00
|
|
|
# NOTE: This operation is indempotent and non-reversible, so be
|
|
|
|
# sure you can deal with any (theoretical) overhead of the the
|
|
|
|
# allocated ``BroadcastReceiver`` before calling this method for
|
|
|
|
# the first time.
|
2021-08-15 21:42:10 +00:00
|
|
|
if self._broadcaster is None:
|
2021-08-19 16:36:05 +00:00
|
|
|
|
|
|
|
bcast = self._broadcaster = broadcast_receiver(
|
2021-08-15 21:42:10 +00:00
|
|
|
self,
|
2021-08-19 16:36:05 +00:00
|
|
|
# use memory channel size by default
|
2021-08-16 16:47:49 +00:00
|
|
|
self._rx_chan._state.max_buffer_size, # type: ignore
|
2021-08-31 17:06:17 +00:00
|
|
|
receive_afunc=self.receive,
|
2021-08-15 21:42:10 +00:00
|
|
|
)
|
2021-08-16 16:47:49 +00:00
|
|
|
|
|
|
|
# NOTE: we override the original stream instance's receive
|
|
|
|
# method to now delegate to the broadcaster's ``.receive()``
|
|
|
|
# such that new subscribers will be copied received values
|
|
|
|
# and this stream doesn't have to expect it's original
|
|
|
|
# consumer(s) to get a new broadcast rx handle.
|
2021-08-19 16:36:05 +00:00
|
|
|
self.receive = bcast.receive # type: ignore
|
2021-08-16 16:47:49 +00:00
|
|
|
# seems there's no graceful way to type this with ``mypy``?
|
|
|
|
# https://github.com/python/mypy/issues/708
|
2021-08-15 21:42:10 +00:00
|
|
|
|
|
|
|
async with self._broadcaster.subscribe() as bstream:
|
2021-08-31 17:06:17 +00:00
|
|
|
assert bstream.key != self._broadcaster.key
|
|
|
|
assert bstream._recv == self._broadcaster._recv
|
2021-08-15 21:42:10 +00:00
|
|
|
yield bstream
|
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
class MsgStream(ReceiveMsgStream, trio.abc.Channel):
|
2021-12-06 00:19:53 +00:00
|
|
|
'''
|
2021-05-01 19:10:03 +00:00
|
|
|
Bidirectional message stream for use within an inter-actor actor
|
|
|
|
``Context```.
|
|
|
|
|
2021-12-06 00:19:53 +00:00
|
|
|
'''
|
2021-05-01 19:10:03 +00:00
|
|
|
async def send(
|
|
|
|
self,
|
|
|
|
data: Any
|
|
|
|
) -> None:
|
2021-06-14 20:34:44 +00:00
|
|
|
'''
|
2021-12-15 21:22:04 +00:00
|
|
|
Send a message over this stream to the far end.
|
2021-12-06 15:57:58 +00:00
|
|
|
|
2021-12-15 21:22:04 +00:00
|
|
|
'''
|
2021-12-06 15:57:58 +00:00
|
|
|
if self._ctx._error:
|
|
|
|
raise self._ctx._error # from None
|
2021-10-04 14:20:49 +00:00
|
|
|
|
2021-12-15 21:22:04 +00:00
|
|
|
if self._closed:
|
|
|
|
raise trio.ClosedResourceError('This stream was already closed')
|
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
await self._ctx.chan.send({'yield': data, 'cid': self._ctx.cid})
|
|
|
|
|
2021-04-27 14:03:00 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
@dataclass
|
2021-05-01 19:10:03 +00:00
|
|
|
class Context:
|
2021-12-03 19:27:04 +00:00
|
|
|
'''
|
|
|
|
An inter-actor, ``trio`` task communication context.
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-12-06 15:57:58 +00:00
|
|
|
NB: This class should never be instatiated directly, it is delivered
|
|
|
|
by either runtime machinery to a remotely started task or by entering
|
|
|
|
``Portal.open_context()``.
|
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
Allows maintaining task or protocol specific state between
|
|
|
|
2 communicating actor tasks. A unique context is created on the
|
|
|
|
callee side/end for every request to a remote actor from a portal.
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
A context can be cancelled and (possibly eventually restarted) from
|
2021-12-03 19:27:04 +00:00
|
|
|
either side of the underlying IPC channel, open task oriented
|
|
|
|
message streams and acts as an IPC aware inter-actor-task cancel
|
|
|
|
scope.
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
'''
|
2021-05-01 19:10:03 +00:00
|
|
|
chan: Channel
|
|
|
|
cid: str
|
|
|
|
|
2021-12-03 19:27:04 +00:00
|
|
|
# these are the "feeder" channels for delivering
|
|
|
|
# message values to the local task from the runtime
|
|
|
|
# msg processing loop.
|
2021-12-03 21:51:15 +00:00
|
|
|
_recv_chan: trio.MemoryReceiveChannel
|
|
|
|
_send_chan: trio.MemorySendChannel
|
|
|
|
|
|
|
|
_remote_func_type: Optional[str] = None
|
2021-12-03 19:27:04 +00:00
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
# only set on the caller side
|
|
|
|
_portal: Optional['Portal'] = None # type: ignore # noqa
|
2021-06-13 23:58:52 +00:00
|
|
|
_result: Optional[Any] = False
|
2021-12-06 00:19:53 +00:00
|
|
|
_error: Optional[BaseException] = None
|
2021-12-03 19:27:04 +00:00
|
|
|
|
|
|
|
# status flags
|
2021-06-13 23:58:52 +00:00
|
|
|
_cancel_called: bool = False
|
2021-11-05 15:36:25 +00:00
|
|
|
_started_called: bool = False
|
|
|
|
_started_received: bool = False
|
2021-12-03 19:27:04 +00:00
|
|
|
_stream_opened: bool = False
|
2021-05-01 19:10:03 +00:00
|
|
|
|
|
|
|
# only set on the callee side
|
2021-06-24 23:56:05 +00:00
|
|
|
_scope_nursery: Optional[trio.Nursery] = None
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-12-06 00:19:53 +00:00
|
|
|
_backpressure: bool = False
|
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
async def send_yield(self, data: Any) -> None:
|
|
|
|
|
|
|
|
warnings.warn(
|
|
|
|
"`Context.send_yield()` is now deprecated. "
|
|
|
|
"Use ``MessageStream.send()``. ",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
await self.chan.send({'yield': data, 'cid': self.cid})
|
|
|
|
|
|
|
|
async def send_stop(self) -> None:
|
|
|
|
await self.chan.send({'stop': True, 'cid': self.cid})
|
|
|
|
|
2021-12-06 20:52:23 +00:00
|
|
|
async def _maybe_raise_from_remote_msg(
|
2021-06-24 23:56:05 +00:00
|
|
|
self,
|
2022-09-15 20:56:50 +00:00
|
|
|
msg: dict[str, Any],
|
2021-06-24 23:56:05 +00:00
|
|
|
|
|
|
|
) -> None:
|
2021-12-06 00:19:53 +00:00
|
|
|
'''
|
2021-12-06 20:52:23 +00:00
|
|
|
(Maybe) unpack and raise a msg error into the local scope
|
2021-06-28 04:18:28 +00:00
|
|
|
nursery for this context.
|
|
|
|
|
|
|
|
Acts as a form of "relay" for a remote error raised
|
|
|
|
in the corresponding remote callee task.
|
2021-12-06 15:57:58 +00:00
|
|
|
|
2021-06-28 04:18:28 +00:00
|
|
|
'''
|
2021-12-06 20:52:23 +00:00
|
|
|
error = msg.get('error')
|
|
|
|
if error:
|
|
|
|
# If this is an error message from a context opened by
|
|
|
|
# ``Portal.open_context()`` we want to interrupt any ongoing
|
|
|
|
# (child) tasks within that context to be notified of the remote
|
|
|
|
# error relayed here.
|
|
|
|
#
|
|
|
|
# The reason we may want to raise the remote error immediately
|
|
|
|
# is that there is no guarantee the associated local task(s)
|
|
|
|
# will attempt to read from any locally opened stream any time
|
|
|
|
# soon.
|
|
|
|
#
|
|
|
|
# NOTE: this only applies when
|
|
|
|
# ``Portal.open_context()`` has been called since it is assumed
|
|
|
|
# (currently) that other portal APIs (``Portal.run()``,
|
|
|
|
# ``.run_in_actor()``) do their own error checking at the point
|
|
|
|
# of the call and result processing.
|
|
|
|
log.error(
|
|
|
|
f'Remote context error for {self.chan.uid}:{self.cid}:\n'
|
|
|
|
f'{msg["error"]["tb_str"]}'
|
|
|
|
)
|
2021-12-15 04:05:30 +00:00
|
|
|
error = unpack_error(msg, self.chan)
|
|
|
|
if (
|
|
|
|
isinstance(error, ContextCancelled) and
|
|
|
|
self._cancel_called
|
|
|
|
):
|
|
|
|
# this is an expected cancel request response message
|
|
|
|
# and we don't need to raise it in scope since it will
|
|
|
|
# potentially override a real error
|
|
|
|
return
|
|
|
|
|
|
|
|
self._error = error
|
2021-12-06 00:19:53 +00:00
|
|
|
|
2021-12-06 20:52:23 +00:00
|
|
|
# TODO: tempted to **not** do this by-reraising in a
|
|
|
|
# nursery and instead cancel a surrounding scope, detect
|
|
|
|
# the cancellation, then lookup the error that was set?
|
|
|
|
if self._scope_nursery:
|
2021-07-08 16:48:34 +00:00
|
|
|
|
2021-12-06 20:52:23 +00:00
|
|
|
async def raiser():
|
|
|
|
raise self._error from None
|
2021-06-24 23:56:05 +00:00
|
|
|
|
2021-12-06 20:52:23 +00:00
|
|
|
# from trio.testing import wait_all_tasks_blocked
|
|
|
|
# await wait_all_tasks_blocked()
|
|
|
|
if not self._scope_nursery._closed: # type: ignore
|
|
|
|
self._scope_nursery.start_soon(raiser)
|
2021-06-24 23:56:05 +00:00
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
async def cancel(self) -> None:
|
2021-12-06 15:57:58 +00:00
|
|
|
'''
|
|
|
|
Cancel this inter-actor-task context.
|
2021-05-01 19:10:03 +00:00
|
|
|
|
|
|
|
Request that the far side cancel it's current linked context,
|
2021-06-13 23:58:52 +00:00
|
|
|
Timeout quickly in an attempt to sidestep 2-generals...
|
|
|
|
|
|
|
|
'''
|
2021-06-28 04:18:28 +00:00
|
|
|
side = 'caller' if self._portal else 'callee'
|
|
|
|
|
2021-10-05 23:25:28 +00:00
|
|
|
log.cancel(f'Cancelling {side} side of context to {self.chan.uid}')
|
2021-06-27 04:46:36 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
self._cancel_called = True
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-06-28 04:18:28 +00:00
|
|
|
if side == 'caller':
|
2021-05-01 19:10:03 +00:00
|
|
|
if not self._portal:
|
|
|
|
raise RuntimeError(
|
|
|
|
"No portal found, this is likely a callee side context"
|
|
|
|
)
|
|
|
|
|
|
|
|
cid = self.cid
|
|
|
|
with trio.move_on_after(0.5) as cs:
|
|
|
|
cs.shield = True
|
2021-10-05 23:25:28 +00:00
|
|
|
log.cancel(
|
2021-05-01 19:10:03 +00:00
|
|
|
f"Cancelling stream {cid} to "
|
|
|
|
f"{self._portal.channel.uid}")
|
|
|
|
|
|
|
|
# NOTE: we're telling the far end actor to cancel a task
|
|
|
|
# corresponding to *this actor*. The far end local channel
|
|
|
|
# instance is passed to `Actor._cancel_task()` implicitly.
|
|
|
|
await self._portal.run_from_ns('self', '_cancel_task', cid=cid)
|
|
|
|
|
|
|
|
if cs.cancelled_caught:
|
|
|
|
# XXX: there's no way to know if the remote task was indeed
|
|
|
|
# cancelled in the case where the connection is broken or
|
|
|
|
# some other network error occurred.
|
|
|
|
# if not self._portal.channel.connected():
|
|
|
|
if not self.chan.connected():
|
2021-10-05 23:25:28 +00:00
|
|
|
log.cancel(
|
2021-05-01 19:10:03 +00:00
|
|
|
"May have failed to cancel remote task "
|
|
|
|
f"{cid} for {self._portal.channel.uid}")
|
2021-10-04 14:20:49 +00:00
|
|
|
else:
|
2021-10-05 23:25:28 +00:00
|
|
|
log.cancel(
|
2021-10-04 14:20:49 +00:00
|
|
|
"Timed out on cancelling remote task "
|
|
|
|
f"{cid} for {self._portal.channel.uid}")
|
2021-05-01 19:10:03 +00:00
|
|
|
else:
|
2021-06-28 04:18:28 +00:00
|
|
|
# callee side remote task
|
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
# TODO: should we have an explicit cancel message
|
|
|
|
# or is relaying the local `trio.Cancelled` as an
|
|
|
|
# {'error': trio.Cancelled, cid: "blah"} enough?
|
|
|
|
# This probably gets into the discussion in
|
|
|
|
# https://github.com/goodboy/tractor/issues/36
|
2021-07-08 16:48:34 +00:00
|
|
|
assert self._scope_nursery
|
2021-06-24 23:56:05 +00:00
|
|
|
self._scope_nursery.cancel_scope.cancel()
|
|
|
|
|
|
|
|
if self._recv_chan:
|
|
|
|
await self._recv_chan.aclose()
|
2021-05-01 19:10:03 +00:00
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
async def open_stream(
|
2021-06-13 23:58:52 +00:00
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
self,
|
2021-12-06 15:57:58 +00:00
|
|
|
backpressure: Optional[bool] = True,
|
2021-12-06 00:19:53 +00:00
|
|
|
msg_buffer_size: Optional[int] = None,
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
) -> AsyncGenerator[MsgStream, None]:
|
2021-12-03 19:27:04 +00:00
|
|
|
'''
|
|
|
|
Open a ``MsgStream``, a bi-directional stream connected to the
|
2021-06-13 23:58:52 +00:00
|
|
|
cross-actor (far end) task for this ``Context``.
|
|
|
|
|
|
|
|
This context manager must be entered on both the caller and
|
|
|
|
callee for the stream to logically be considered "connected".
|
|
|
|
|
|
|
|
A ``MsgStream`` is currently "one-shot" use, meaning if you
|
|
|
|
close it you can not "re-open" it for streaming and instead you
|
|
|
|
must re-establish a new surrounding ``Context`` using
|
|
|
|
``Portal.open_context()``. In the future this may change but
|
|
|
|
currently there seems to be no obvious reason to support
|
|
|
|
"re-opening":
|
|
|
|
- pausing a stream can be done with a message.
|
|
|
|
- task errors will normally require a restart of the entire
|
|
|
|
scope of the inter-actor task context due to the nature of
|
|
|
|
``trio``'s cancellation system.
|
|
|
|
|
|
|
|
'''
|
2021-05-01 19:10:03 +00:00
|
|
|
actor = current_actor()
|
|
|
|
|
|
|
|
# here we create a mem chan that corresponds to the
|
|
|
|
# far end caller / callee.
|
|
|
|
|
2021-06-30 17:47:38 +00:00
|
|
|
# Likewise if the surrounding context has been cancelled we error here
|
|
|
|
# since it likely means the surrounding block was exited or
|
|
|
|
# killed
|
|
|
|
|
|
|
|
if self._cancel_called:
|
2021-06-13 23:58:52 +00:00
|
|
|
task = trio.lowlevel.current_task().name
|
2021-06-30 17:47:38 +00:00
|
|
|
raise ContextCancelled(
|
|
|
|
f'Context around {actor.uid[0]}:{task} was already cancelled!'
|
2021-06-13 23:58:52 +00:00
|
|
|
)
|
|
|
|
|
2021-11-05 15:36:25 +00:00
|
|
|
if not self._portal and not self._started_called:
|
|
|
|
raise RuntimeError(
|
|
|
|
'Context.started()` must be called before opening a stream'
|
|
|
|
)
|
|
|
|
|
2021-10-04 14:20:49 +00:00
|
|
|
# NOTE: in one way streaming this only happens on the
|
2021-12-03 21:51:15 +00:00
|
|
|
# caller side inside `Actor.start_remote_task()` so if you try
|
2021-10-04 14:20:49 +00:00
|
|
|
# to send a stop from the caller to the callee in the
|
|
|
|
# single-direction-stream case you'll get a lookup error
|
|
|
|
# currently.
|
2021-12-03 19:27:04 +00:00
|
|
|
ctx = actor.get_context(
|
|
|
|
self.chan,
|
|
|
|
self.cid,
|
2021-12-06 00:19:53 +00:00
|
|
|
msg_buffer_size=msg_buffer_size,
|
2021-10-04 14:20:49 +00:00
|
|
|
)
|
2021-12-06 00:19:53 +00:00
|
|
|
ctx._backpressure = backpressure
|
2021-12-03 19:27:04 +00:00
|
|
|
assert ctx is self
|
2021-10-04 14:20:49 +00:00
|
|
|
|
2021-06-30 17:47:38 +00:00
|
|
|
# XXX: If the underlying channel feeder receive mem chan has
|
|
|
|
# been closed then likely client code has already exited
|
|
|
|
# a ``.open_stream()`` block prior or there was some other
|
|
|
|
# unanticipated error or cancellation from ``trio``.
|
|
|
|
|
2021-12-03 19:27:04 +00:00
|
|
|
if ctx._recv_chan._closed:
|
2021-06-30 17:47:38 +00:00
|
|
|
raise trio.ClosedResourceError(
|
|
|
|
'The underlying channel for this stream was already closed!?')
|
|
|
|
|
2021-05-07 15:20:51 +00:00
|
|
|
async with MsgStream(
|
|
|
|
ctx=self,
|
2021-12-03 19:27:04 +00:00
|
|
|
rx_chan=ctx._recv_chan,
|
2021-05-07 15:20:51 +00:00
|
|
|
) as rchan:
|
2021-05-01 19:10:03 +00:00
|
|
|
|
|
|
|
if self._portal:
|
|
|
|
self._portal._streams.add(rchan)
|
|
|
|
|
|
|
|
try:
|
2021-12-03 19:27:04 +00:00
|
|
|
self._stream_opened = True
|
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
# ensure we aren't cancelled before delivering
|
|
|
|
# the stream
|
|
|
|
# await trio.lowlevel.checkpoint()
|
2021-05-01 19:10:03 +00:00
|
|
|
yield rchan
|
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
# XXX: Make the stream "one-shot use". On exit, signal
|
|
|
|
# ``trio.EndOfChannel``/``StopAsyncIteration`` to the
|
|
|
|
# far end.
|
2021-05-01 19:10:03 +00:00
|
|
|
await self.send_stop()
|
2021-05-07 15:20:51 +00:00
|
|
|
|
2021-05-12 03:42:34 +00:00
|
|
|
finally:
|
2021-05-01 19:10:03 +00:00
|
|
|
if self._portal:
|
2022-05-14 21:42:03 +00:00
|
|
|
try:
|
|
|
|
self._portal._streams.remove(stream)
|
|
|
|
except KeyError:
|
|
|
|
log.warning(
|
|
|
|
f'Stream was already destroyed?\n'
|
|
|
|
f'actor: {self.chan.uid}\n'
|
|
|
|
f'ctx id: {self.cid}'
|
|
|
|
)
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
async def result(self) -> Any:
|
2022-02-14 13:38:19 +00:00
|
|
|
'''
|
|
|
|
From a caller side, wait for and return the final result from
|
2021-06-13 23:58:52 +00:00
|
|
|
the callee side task.
|
|
|
|
|
|
|
|
'''
|
|
|
|
assert self._portal, "Context.result() can not be called from callee!"
|
|
|
|
assert self._recv_chan
|
|
|
|
|
|
|
|
if self._result is False:
|
|
|
|
|
|
|
|
if not self._recv_chan._closed: # type: ignore
|
|
|
|
|
|
|
|
# wait for a final context result consuming
|
|
|
|
# and discarding any bi dir stream msgs still
|
|
|
|
# in transit from the far end.
|
|
|
|
while True:
|
|
|
|
|
|
|
|
msg = await self._recv_chan.receive()
|
|
|
|
try:
|
|
|
|
self._result = msg['return']
|
|
|
|
break
|
2021-12-06 15:57:58 +00:00
|
|
|
except KeyError as msgerr:
|
2021-06-13 23:58:52 +00:00
|
|
|
|
|
|
|
if 'yield' in msg:
|
2021-10-05 23:25:28 +00:00
|
|
|
# far end task is still streaming to us so discard
|
|
|
|
log.warning(f'Discarding stream delivered {msg}')
|
2021-06-13 23:58:52 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
elif 'stop' in msg:
|
|
|
|
log.debug('Remote stream terminated')
|
|
|
|
continue
|
|
|
|
|
|
|
|
# internal error should never get here
|
|
|
|
assert msg.get('cid'), (
|
|
|
|
"Received internal error at portal?")
|
2021-12-06 15:57:58 +00:00
|
|
|
|
|
|
|
raise unpack_error(
|
|
|
|
msg, self._portal.channel
|
|
|
|
) from msgerr
|
2021-06-13 23:58:52 +00:00
|
|
|
|
|
|
|
return self._result
|
|
|
|
|
2021-11-05 15:36:25 +00:00
|
|
|
async def started(
|
|
|
|
self,
|
|
|
|
value: Optional[Any] = None
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-11-05 15:36:25 +00:00
|
|
|
) -> None:
|
|
|
|
'''
|
|
|
|
Indicate to calling actor's task that this linked context
|
|
|
|
has started and send ``value`` to the other side.
|
|
|
|
|
|
|
|
On the calling side ``value`` is the second item delivered
|
|
|
|
in the tuple returned by ``Portal.open_context()``.
|
|
|
|
|
|
|
|
'''
|
2021-05-01 19:10:03 +00:00
|
|
|
if self._portal:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Caller side context {self} can not call started!")
|
|
|
|
|
2021-11-05 15:36:25 +00:00
|
|
|
elif self._started_called:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"called 'started' twice on context with {self.chan.uid}")
|
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
await self.chan.send({'started': value, 'cid': self.cid})
|
2021-11-05 15:36:25 +00:00
|
|
|
self._started_called = True
|
2021-05-01 19:10:03 +00:00
|
|
|
|
2021-06-13 23:58:52 +00:00
|
|
|
# TODO: do we need a restart api?
|
|
|
|
# async def restart(self) -> None:
|
|
|
|
# pass
|
|
|
|
|
2021-05-01 19:10:03 +00:00
|
|
|
|
|
|
|
def stream(func: Callable) -> Callable:
|
|
|
|
"""Mark an async function as a streaming routine with ``@stream``.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# annotate
|
2021-05-07 15:52:08 +00:00
|
|
|
# TODO: apply whatever solution ``mypy`` ends up picking for this:
|
|
|
|
# https://github.com/python/mypy/issues/2087#issuecomment-769266912
|
|
|
|
func._tractor_stream_function = True # type: ignore
|
2021-05-01 19:10:03 +00:00
|
|
|
|
|
|
|
sig = inspect.signature(func)
|
|
|
|
params = sig.parameters
|
|
|
|
if 'stream' not in params and 'ctx' in params:
|
|
|
|
warnings.warn(
|
|
|
|
"`@tractor.stream decorated funcs should now declare a `stream` "
|
|
|
|
" arg, `ctx` is now designated for use with @tractor.context",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
'ctx' not in params and
|
|
|
|
'to_trio' not in params and
|
|
|
|
'stream' not in params
|
|
|
|
):
|
|
|
|
raise TypeError(
|
|
|
|
"The first argument to the stream function "
|
|
|
|
f"{func.__name__} must be `ctx: tractor.Context` "
|
|
|
|
"(Or ``to_trio`` if using ``asyncio`` in guest mode)."
|
|
|
|
)
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
|
def context(func: Callable) -> Callable:
|
|
|
|
"""Mark an async function as a streaming routine with ``@context``.
|
|
|
|
|
|
|
|
"""
|
|
|
|
# annotate
|
2021-05-07 15:52:08 +00:00
|
|
|
# TODO: apply whatever solution ``mypy`` ends up picking for this:
|
|
|
|
# https://github.com/python/mypy/issues/2087#issuecomment-769266912
|
|
|
|
func._tractor_context_function = True # type: ignore
|
2021-05-01 19:10:03 +00:00
|
|
|
|
|
|
|
sig = inspect.signature(func)
|
|
|
|
params = sig.parameters
|
|
|
|
if 'ctx' not in params:
|
|
|
|
raise TypeError(
|
|
|
|
"The first argument to the context function "
|
|
|
|
f"{func.__name__} must be `ctx: tractor.Context`"
|
|
|
|
)
|
|
|
|
return func
|