Skip to content

Commit b06754d

Browse files
committed
WIP tagged union message type API
XXX NOTE XXX: this is a heavily modified commit from the original (ec22646) which was super out of date when rebased onto the current branch. I went through a manual conflict rework and removed all the legacy segments as well as rename-moved this original mod `tractor.msg.py` -> `tractor.msg/_old_msg.py`. Further the `NamespacePath` type def was discarded from this mod since it was from a super old version which was already moved to a `.msg.ptr` submod. As per original questions and discussion with `msgspec` author: - msgspec/msgspec#25 - msgspec/msgspec#140 this prototypes a new (but very naive) `msgspec.Struct` codec implementation which will be more filled out in the next commit.
1 parent 213e083 commit b06754d

2 files changed

Lines changed: 124 additions & 1 deletion

File tree

‎tractor/_ipc.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ def __init__(
165165
self.codec: MsgCodec = codec or MsgCodec()
166166

167167
async def _iter_packets(self) -> AsyncGenerator[dict, None]:
168-
'''Yield packets from the underlying stream.
168+
'''
169+
Yield `bytes`-blob decoded packets from the underlying TCP
170+
stream using the current task's `MsgCodec`.
169171
170172
'''
171173
import msgspec # noqa

‎tractor/msg/_old_msg.py‎

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# tractor: structured concurrent "actors".
2+
# Copyright 2018-eternity Tyler Goodlet.
3+
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU Affero General Public License as published by
6+
# the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU Affero General Public License for more details.
13+
14+
# You should have received a copy of the GNU Affero General Public License
15+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
17+
'''
18+
Capability-based messaging specifications: or colloquially as "msgspecs".
19+
20+
Includes our SCIPP (structured-con-inter-process-protocol) message type defs
21+
and APIs for applying custom msgspec-sets for implementing un-protocol state machines.
22+
23+
'''
24+
25+
# TODO: integration with our ``enable_modules: list[str]`` caps sys.
26+
27+
# ``pkgutil.resolve_name()`` internally uses
28+
# ``importlib.import_module()`` which can be filtered by inserting
29+
# a ``MetaPathFinder`` into ``sys.meta_path`` (which we could do before
30+
# entering the ``Actor._process_messages()`` loop).
31+
# https://github.com/python/cpython/blob/main/Lib/pkgutil.py#L645
32+
# https://stackoverflow.com/questions/1350466/preventing-python-code-from-importing-certain-modules
33+
# - https://stackoverflow.com/a/63320902
34+
# - https://docs.python.org/3/library/sys.html#sys.meta_path
35+
36+
# the new "Implicit Namespace Packages" might be relevant?
37+
# - https://www.python.org/dev/peps/pep-0420/
38+
39+
# add implicit serialized message type support so that paths can be
40+
# handed directly to IPC primitives such as streams and `Portal.run()`
41+
# calls:
42+
# - via ``msgspec``:
43+
# - https://jcristharif.com/msgspec/api.html#struct
44+
# - https://jcristharif.com/msgspec/extending.html
45+
# via ``msgpack-python``:
46+
# https://github.com/msgpack/msgpack-python#packingunpacking-of-custom-data-type
47+
48+
from __future__ import annotations
49+
from contextlib import contextmanager as cm
50+
from typing import (
51+
Union,
52+
Any,
53+
)
54+
55+
from msgspec import Struct
56+
from msgspec.msgpack import (
57+
Encoder,
58+
Decoder,
59+
)
60+
61+
62+
# LIFO codec stack that is appended when the user opens the
63+
# ``configure_native_msgs()`` cm below to configure a new codec set
64+
# which will be applied to all new (msgspec relevant) IPC transports
65+
# that are spawned **after** the configure call is made.
66+
_lifo_codecs: list[
67+
tuple[
68+
Encoder,
69+
Decoder,
70+
],
71+
] = [(Encoder(), Decoder())]
72+
73+
74+
def get_msg_codecs() -> tuple[
75+
Encoder,
76+
Decoder,
77+
]:
78+
'''
79+
Return the currently configured ``msgspec`` codec set.
80+
81+
The defaults are defined above.
82+
83+
'''
84+
global _lifo_codecs
85+
return _lifo_codecs[-1]
86+
87+
88+
@cm
89+
def configure_native_msgs(
90+
tagged_structs: list[Struct],
91+
):
92+
'''
93+
Push a codec set that will natively decode
94+
tagged structs provied in ``tagged_structs``
95+
in all IPC transports and pop the codec on exit.
96+
97+
'''
98+
global _lifo_codecs
99+
100+
# See "tagged unions" docs:
101+
# https://jcristharif.com/msgspec/structs.html#tagged-unions
102+
103+
# "The quickest way to enable tagged unions is to set tag=True when
104+
# defining every struct type in the union. In this case tag_field
105+
# defaults to "type", and tag defaults to the struct class name
106+
# (e.g. "Get")."
107+
enc = Encoder()
108+
109+
types_union = Union[tagged_structs[0]] | Any
110+
for struct in tagged_structs[1:]:
111+
types_union |= struct
112+
113+
dec = Decoder(types_union)
114+
115+
_lifo_codecs.append((enc, dec))
116+
try:
117+
print("YOYOYOOYOYOYOY")
118+
yield enc, dec
119+
finally:
120+
print("NONONONONON")
121+
_lifo_codecs.pop()

0 commit comments

Comments
 (0)