Compare commits
52 Commits
main
...
multihomed
Author | SHA1 | Date |
---|---|---|
|
84171c2d10 | |
|
1dd9c4ae75 | |
|
8dd7bc0ba9 | |
|
aaba298361 | |
|
7ceeae9390 | |
|
8b74d9ff51 | |
|
75522c80b8 | |
|
7eea1ec61a | |
|
5892d15579 | |
|
382fd91bf3 | |
|
1a1f136c85 | |
|
1d01fa16b1 | |
|
9f126a476a | |
|
ff7dfa9b8e | |
|
85e677bf3b | |
|
2e5ba84270 | |
|
a8c0fc3b79 | |
|
99ccab7574 | |
|
12211c8295 | |
|
63aeac8d4c | |
|
7951251c47 | |
|
07bc675f5e | |
|
4f7d1ea704 | |
|
47d52f3cc0 | |
|
d0876bb0a4 | |
|
850b9999ff | |
|
4571b8cc84 | |
|
db58f6e1b5 | |
|
76b7006977 | |
|
bd1885bce1 | |
|
066a35322e | |
|
2ebc30d708 | |
|
57a5b7eb6f | |
|
e269aa3751 | |
|
7fc9297104 | |
|
9208708b3a | |
|
cf2f2adec2 | |
|
f28abc6720 | |
|
6f33a9891e | |
|
79604b7f98 | |
|
cec4a2a0ab | |
|
4089e4b3ac | |
|
5ec48310b6 | |
|
697900deb1 | |
|
2e55c124b1 | |
|
0f21c8ba6a | |
|
7b7410bc0f | |
|
b59cba74cd | |
|
7e39ef7ed1 | |
|
c8ea0fdf53 | |
|
885319e9ae | |
|
b815b61707 |
|
@ -3,8 +3,8 @@
|
||||||
|gh_actions|
|
|gh_actions|
|
||||||
|docs|
|
|docs|
|
||||||
|
|
||||||
``tractor`` is a `structured concurrent`_, multi-processing_ runtime
|
``tractor`` is a `structured concurrent`_, (optionally
|
||||||
built on trio_.
|
distributed_) multi-processing_ runtime built on trio_.
|
||||||
|
|
||||||
Fundamentally, ``tractor`` gives you parallelism via
|
Fundamentally, ``tractor`` gives you parallelism via
|
||||||
``trio``-"*actors*": independent Python processes (aka
|
``trio``-"*actors*": independent Python processes (aka
|
||||||
|
@ -17,11 +17,20 @@ protocol" constructed on top of multiple Pythons each running a ``trio``
|
||||||
scheduled runtime - a call to ``trio.run()``.
|
scheduled runtime - a call to ``trio.run()``.
|
||||||
|
|
||||||
We believe the system adheres to the `3 axioms`_ of an "`actor model`_"
|
We believe the system adheres to the `3 axioms`_ of an "`actor model`_"
|
||||||
but likely *does not* look like what *you* probably think an "actor
|
but likely **does not** look like what **you** probably *think* an "actor
|
||||||
model" looks like, and that's *intentional*.
|
model" looks like, and that's **intentional**.
|
||||||
|
|
||||||
The first step to grok ``tractor`` is to get the basics of ``trio`` down.
|
|
||||||
A great place to start is the `trio docs`_ and this `blog post`_.
|
Where do i start!?
|
||||||
|
------------------
|
||||||
|
The first step to grok ``tractor`` is to get an intermediate
|
||||||
|
knowledge of ``trio`` and **structured concurrency** B)
|
||||||
|
|
||||||
|
Some great places to start are,
|
||||||
|
- the seminal `blog post`_
|
||||||
|
- obviously the `trio docs`_
|
||||||
|
- wikipedia's nascent SC_ page
|
||||||
|
- the fancy diagrams @ libdill-docs_
|
||||||
|
|
||||||
|
|
||||||
Features
|
Features
|
||||||
|
@ -593,6 +602,7 @@ matrix seems too hip, we're also mostly all in the the `trio gitter
|
||||||
channel`_!
|
channel`_!
|
||||||
|
|
||||||
.. _structured concurrent: https://trio.discourse.group/t/concise-definition-of-structured-concurrency/228
|
.. _structured concurrent: https://trio.discourse.group/t/concise-definition-of-structured-concurrency/228
|
||||||
|
.. _distributed: https://en.wikipedia.org/wiki/Distributed_computing
|
||||||
.. _multi-processing: https://en.wikipedia.org/wiki/Multiprocessing
|
.. _multi-processing: https://en.wikipedia.org/wiki/Multiprocessing
|
||||||
.. _trio: https://github.com/python-trio/trio
|
.. _trio: https://github.com/python-trio/trio
|
||||||
.. _nurseries: https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/#nurseries-a-structured-replacement-for-go-statements
|
.. _nurseries: https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/#nurseries-a-structured-replacement-for-go-statements
|
||||||
|
@ -611,8 +621,9 @@ channel`_!
|
||||||
.. _trio docs: https://trio.readthedocs.io/en/latest/
|
.. _trio docs: https://trio.readthedocs.io/en/latest/
|
||||||
.. _blog post: https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/
|
.. _blog post: https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/
|
||||||
.. _structured concurrency: https://en.wikipedia.org/wiki/Structured_concurrency
|
.. _structured concurrency: https://en.wikipedia.org/wiki/Structured_concurrency
|
||||||
|
.. _SC: https://en.wikipedia.org/wiki/Structured_concurrency
|
||||||
|
.. _libdill-docs: https://sustrik.github.io/libdill/structured-concurrency.html
|
||||||
.. _structured chadcurrency: https://en.wikipedia.org/wiki/Structured_concurrency
|
.. _structured chadcurrency: https://en.wikipedia.org/wiki/Structured_concurrency
|
||||||
.. _structured concurrency: https://en.wikipedia.org/wiki/Structured_concurrency
|
|
||||||
.. _unrequirements: https://en.wikipedia.org/wiki/Actor_model#Direct_communication_and_asynchrony
|
.. _unrequirements: https://en.wikipedia.org/wiki/Actor_model#Direct_communication_and_asynchrony
|
||||||
.. _async generators: https://www.python.org/dev/peps/pep-0525/
|
.. _async generators: https://www.python.org/dev/peps/pep-0525/
|
||||||
.. _trio-parallel: https://github.com/richardsheridan/trio-parallel
|
.. _trio-parallel: https://github.com/richardsheridan/trio-parallel
|
||||||
|
|
|
@ -0,0 +1,117 @@
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import trio
|
||||||
|
import tractor
|
||||||
|
from tractor import to_asyncio
|
||||||
|
|
||||||
|
|
||||||
|
async def aio_sleep_forever():
|
||||||
|
await asyncio.sleep(float('inf'))
|
||||||
|
|
||||||
|
|
||||||
|
async def bp_then_error(
|
||||||
|
to_trio: trio.MemorySendChannel,
|
||||||
|
from_trio: asyncio.Queue,
|
||||||
|
|
||||||
|
raise_after_bp: bool = True,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
# sync with ``trio``-side (caller) task
|
||||||
|
to_trio.send_nowait('start')
|
||||||
|
|
||||||
|
# NOTE: what happens here inside the hook needs some refinement..
|
||||||
|
# => seems like it's still `._debug._set_trace()` but
|
||||||
|
# we set `Lock.local_task_in_debug = 'sync'`, we probably want
|
||||||
|
# some further, at least, meta-data about the task/actoq in debug
|
||||||
|
# in terms of making it clear it's asyncio mucking about.
|
||||||
|
breakpoint()
|
||||||
|
|
||||||
|
# short checkpoint / delay
|
||||||
|
await asyncio.sleep(0.5)
|
||||||
|
|
||||||
|
if raise_after_bp:
|
||||||
|
raise ValueError('blah')
|
||||||
|
|
||||||
|
# TODO: test case with this so that it gets cancelled?
|
||||||
|
else:
|
||||||
|
# XXX NOTE: this is required in order to get the SIGINT-ignored
|
||||||
|
# hang case documented in the module script section!
|
||||||
|
await aio_sleep_forever()
|
||||||
|
|
||||||
|
|
||||||
|
@tractor.context
|
||||||
|
async def trio_ctx(
|
||||||
|
ctx: tractor.Context,
|
||||||
|
bp_before_started: bool = False,
|
||||||
|
):
|
||||||
|
|
||||||
|
# this will block until the ``asyncio`` task sends a "first"
|
||||||
|
# message, see first line in above func.
|
||||||
|
async with (
|
||||||
|
|
||||||
|
to_asyncio.open_channel_from(
|
||||||
|
bp_then_error,
|
||||||
|
raise_after_bp=not bp_before_started,
|
||||||
|
) as (first, chan),
|
||||||
|
|
||||||
|
trio.open_nursery() as n,
|
||||||
|
):
|
||||||
|
|
||||||
|
assert first == 'start'
|
||||||
|
|
||||||
|
if bp_before_started:
|
||||||
|
await tractor.breakpoint()
|
||||||
|
|
||||||
|
await ctx.started(first)
|
||||||
|
|
||||||
|
n.start_soon(
|
||||||
|
to_asyncio.run_task,
|
||||||
|
aio_sleep_forever,
|
||||||
|
)
|
||||||
|
await trio.sleep_forever()
|
||||||
|
|
||||||
|
|
||||||
|
async def main(
|
||||||
|
bps_all_over: bool = False,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
async with tractor.open_nursery() as n:
|
||||||
|
|
||||||
|
p = await n.start_actor(
|
||||||
|
'aio_daemon',
|
||||||
|
enable_modules=[__name__],
|
||||||
|
infect_asyncio=True,
|
||||||
|
debug_mode=True,
|
||||||
|
loglevel='cancel',
|
||||||
|
)
|
||||||
|
|
||||||
|
async with p.open_context(
|
||||||
|
trio_ctx,
|
||||||
|
bp_before_started=bps_all_over,
|
||||||
|
) as (ctx, first):
|
||||||
|
|
||||||
|
assert first == 'start'
|
||||||
|
|
||||||
|
if bps_all_over:
|
||||||
|
await tractor.breakpoint()
|
||||||
|
|
||||||
|
# await trio.sleep_forever()
|
||||||
|
await ctx.cancel()
|
||||||
|
assert 0
|
||||||
|
|
||||||
|
# TODO: case where we cancel from trio-side while asyncio task
|
||||||
|
# has debugger lock?
|
||||||
|
# await p.cancel_actor()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
# works fine B)
|
||||||
|
trio.run(main)
|
||||||
|
|
||||||
|
# will hang and ignores SIGINT !!
|
||||||
|
# NOTE: you'll need to send a SIGQUIT (via ctl-\) to kill it
|
||||||
|
# manually..
|
||||||
|
# trio.run(main, True)
|
|
@ -0,0 +1,8 @@
|
||||||
|
# vim: ft=ini
|
||||||
|
# pytest.ini for tractor
|
||||||
|
|
||||||
|
[pytest]
|
||||||
|
# don't show frickin captured logs AGAIN in the report..
|
||||||
|
addopts = --show-capture='no'
|
||||||
|
log_cli = false
|
||||||
|
; minversion = 6.0
|
13
setup.py
13
setup.py
|
@ -26,7 +26,7 @@ with open('docs/README.rst', encoding='utf-8') as f:
|
||||||
setup(
|
setup(
|
||||||
name="tractor",
|
name="tractor",
|
||||||
version='0.1.0a6dev0', # alpha zone
|
version='0.1.0a6dev0', # alpha zone
|
||||||
description='structured concurrrent `trio`-"actors"',
|
description='structured concurrent `trio`-"actors"',
|
||||||
long_description=readme,
|
long_description=readme,
|
||||||
license='AGPLv3',
|
license='AGPLv3',
|
||||||
author='Tyler Goodlet',
|
author='Tyler Goodlet',
|
||||||
|
@ -39,6 +39,7 @@ setup(
|
||||||
'tractor.experimental', # wacky ideas
|
'tractor.experimental', # wacky ideas
|
||||||
'tractor.trionics', # trio extensions
|
'tractor.trionics', # trio extensions
|
||||||
'tractor.msg', # lowlevel data types
|
'tractor.msg', # lowlevel data types
|
||||||
|
'tractor.devx', # "dev-experience"
|
||||||
],
|
],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
|
|
||||||
|
@ -52,6 +53,7 @@ setup(
|
||||||
# 'exceptiongroup', # in stdlib as of 3.11!
|
# 'exceptiongroup', # in stdlib as of 3.11!
|
||||||
|
|
||||||
# tooling
|
# tooling
|
||||||
|
'stackscope',
|
||||||
'tricycle',
|
'tricycle',
|
||||||
'trio_typing',
|
'trio_typing',
|
||||||
'colorlog',
|
'colorlog',
|
||||||
|
@ -63,16 +65,15 @@ setup(
|
||||||
# debug mode REPL
|
# debug mode REPL
|
||||||
'pdbp',
|
'pdbp',
|
||||||
|
|
||||||
|
# TODO: distributed transport using
|
||||||
|
# linux kernel networking
|
||||||
|
# 'pyroute2',
|
||||||
|
|
||||||
# pip ref docs on these specs:
|
# pip ref docs on these specs:
|
||||||
# https://pip.pypa.io/en/stable/reference/requirement-specifiers/#examples
|
# https://pip.pypa.io/en/stable/reference/requirement-specifiers/#examples
|
||||||
# and pep:
|
# and pep:
|
||||||
# https://peps.python.org/pep-0440/#version-specifiers
|
# https://peps.python.org/pep-0440/#version-specifiers
|
||||||
|
|
||||||
# windows deps workaround for ``pdbpp``
|
|
||||||
# https://github.com/pdbpp/pdbpp/issues/498
|
|
||||||
# https://github.com/pdbpp/fancycompleter/issues/37
|
|
||||||
'pyreadline3 ; platform_system == "Windows"',
|
|
||||||
|
|
||||||
],
|
],
|
||||||
tests_require=['pytest'],
|
tests_require=['pytest'],
|
||||||
python_requires=">=3.10",
|
python_requires=">=3.10",
|
||||||
|
|
|
@ -93,12 +93,18 @@ _reg_addr: tuple[str, int] = (
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
random.randint(1000, 9999),
|
random.randint(1000, 9999),
|
||||||
)
|
)
|
||||||
_arb_addr = _reg_addr
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='session')
|
@pytest.fixture(scope='session')
|
||||||
def arb_addr():
|
def reg_addr() -> tuple[str, int]:
|
||||||
return _arb_addr
|
|
||||||
|
# globally override the runtime to the per-test-session-dynamic
|
||||||
|
# addr so that all tests never conflict with any other actor
|
||||||
|
# tree using the default.
|
||||||
|
from tractor import _root
|
||||||
|
_root._default_lo_addrs = [_reg_addr]
|
||||||
|
|
||||||
|
return _reg_addr
|
||||||
|
|
||||||
|
|
||||||
def pytest_generate_tests(metafunc):
|
def pytest_generate_tests(metafunc):
|
||||||
|
@ -140,30 +146,35 @@ def sig_prog(proc, sig):
|
||||||
def daemon(
|
def daemon(
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
testdir,
|
testdir,
|
||||||
arb_addr: tuple[str, int],
|
reg_addr: tuple[str, int],
|
||||||
):
|
):
|
||||||
'''
|
'''
|
||||||
Run a daemon actor as a "remote arbiter".
|
Run a daemon root actor as a separate actor-process tree and
|
||||||
|
"remote registrar" for discovery-protocol related tests.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if loglevel in ('trace', 'debug'):
|
if loglevel in ('trace', 'debug'):
|
||||||
# too much logging will lock up the subproc (smh)
|
# XXX: too much logging will lock up the subproc (smh)
|
||||||
loglevel = 'info'
|
loglevel: str = 'info'
|
||||||
|
|
||||||
cmdargs = [
|
code: str = (
|
||||||
sys.executable, '-c',
|
"import tractor; "
|
||||||
"import tractor; tractor.run_daemon([], registry_addr={}, loglevel={})"
|
"tractor.run_daemon([], registry_addrs={reg_addrs}, loglevel={ll})"
|
||||||
.format(
|
).format(
|
||||||
arb_addr,
|
reg_addrs=str([reg_addr]),
|
||||||
"'{}'".format(loglevel) if loglevel else None)
|
ll="'{}'".format(loglevel) if loglevel else None,
|
||||||
|
)
|
||||||
|
cmd: list[str] = [
|
||||||
|
sys.executable,
|
||||||
|
'-c', code,
|
||||||
]
|
]
|
||||||
kwargs = dict()
|
kwargs = {}
|
||||||
if platform.system() == 'Windows':
|
if platform.system() == 'Windows':
|
||||||
# without this, tests hang on windows forever
|
# without this, tests hang on windows forever
|
||||||
kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
|
kwargs['creationflags'] = subprocess.CREATE_NEW_PROCESS_GROUP
|
||||||
|
|
||||||
proc = testdir.popen(
|
proc = testdir.popen(
|
||||||
cmdargs,
|
cmd,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
**kwargs,
|
**kwargs,
|
||||||
|
|
|
@ -10,6 +10,7 @@ TODO:
|
||||||
- wonder if any of it'll work on OS X?
|
- wonder if any of it'll work on OS X?
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from functools import partial
|
||||||
import itertools
|
import itertools
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import platform
|
import platform
|
||||||
|
@ -26,6 +27,10 @@ from pexpect.exceptions import (
|
||||||
from tractor._testing import (
|
from tractor._testing import (
|
||||||
examples_dir,
|
examples_dir,
|
||||||
)
|
)
|
||||||
|
from tractor.devx._debug import (
|
||||||
|
_pause_msg,
|
||||||
|
_crash_msg,
|
||||||
|
)
|
||||||
from conftest import (
|
from conftest import (
|
||||||
_ci_env,
|
_ci_env,
|
||||||
)
|
)
|
||||||
|
@ -123,20 +128,52 @@ def expect(
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def in_prompt_msg(
|
||||||
|
prompt: str,
|
||||||
|
parts: list[str],
|
||||||
|
|
||||||
|
pause_on_false: bool = False,
|
||||||
|
print_prompt_on_false: bool = True,
|
||||||
|
|
||||||
|
) -> bool:
|
||||||
|
'''
|
||||||
|
Predicate check if (the prompt's) std-streams output has all
|
||||||
|
`str`-parts in it.
|
||||||
|
|
||||||
|
Can be used in test asserts for bulk matching expected
|
||||||
|
log/REPL output for a given `pdb` interact point.
|
||||||
|
|
||||||
|
'''
|
||||||
|
for part in parts:
|
||||||
|
if part not in prompt:
|
||||||
|
|
||||||
|
if pause_on_false:
|
||||||
|
import pdbp
|
||||||
|
pdbp.set_trace()
|
||||||
|
|
||||||
|
if print_prompt_on_false:
|
||||||
|
print(prompt)
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def assert_before(
|
def assert_before(
|
||||||
child,
|
child,
|
||||||
patts: list[str],
|
patts: list[str],
|
||||||
|
|
||||||
|
**kwargs,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
before = str(child.before.decode())
|
# as in before the prompt end
|
||||||
|
before: str = str(child.before.decode())
|
||||||
|
assert in_prompt_msg(
|
||||||
|
prompt=before,
|
||||||
|
parts=patts,
|
||||||
|
|
||||||
for patt in patts:
|
**kwargs
|
||||||
try:
|
)
|
||||||
assert patt in before
|
|
||||||
except AssertionError:
|
|
||||||
print(before)
|
|
||||||
raise
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(
|
@pytest.fixture(
|
||||||
|
@ -166,7 +203,7 @@ def ctlc(
|
||||||
# XXX: disable pygments highlighting for auto-tests
|
# XXX: disable pygments highlighting for auto-tests
|
||||||
# since some envs (like actions CI) will struggle
|
# since some envs (like actions CI) will struggle
|
||||||
# the the added color-char encoding..
|
# the the added color-char encoding..
|
||||||
from tractor._debug import TractorConfig
|
from tractor.devx._debug import TractorConfig
|
||||||
TractorConfig.use_pygements = False
|
TractorConfig.use_pygements = False
|
||||||
|
|
||||||
yield use_ctlc
|
yield use_ctlc
|
||||||
|
@ -195,7 +232,10 @@ def test_root_actor_error(spawn, user_in_out):
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
|
|
||||||
# make sure expected logging and error arrives
|
# make sure expected logging and error arrives
|
||||||
assert "Attaching to pdb in crashed actor: ('root'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_crash_msg, "('root'"]
|
||||||
|
)
|
||||||
assert 'AssertionError' in before
|
assert 'AssertionError' in before
|
||||||
|
|
||||||
# send user command
|
# send user command
|
||||||
|
@ -332,7 +372,10 @@ def test_subactor_error(
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
|
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert "Attaching to pdb in crashed actor: ('name_error'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_crash_msg, "('name_error'"]
|
||||||
|
)
|
||||||
|
|
||||||
if do_next:
|
if do_next:
|
||||||
child.sendline('n')
|
child.sendline('n')
|
||||||
|
@ -353,9 +396,15 @@ def test_subactor_error(
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
|
|
||||||
# root actor gets debugger engaged
|
# root actor gets debugger engaged
|
||||||
assert "Attaching to pdb in crashed actor: ('root'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_crash_msg, "('root'"]
|
||||||
|
)
|
||||||
# error is a remote error propagated from the subactor
|
# error is a remote error propagated from the subactor
|
||||||
assert "RemoteActorError: ('name_error'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_crash_msg, "('name_error'"]
|
||||||
|
)
|
||||||
|
|
||||||
# another round
|
# another round
|
||||||
if ctlc:
|
if ctlc:
|
||||||
|
@ -380,7 +429,10 @@ def test_subactor_breakpoint(
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
|
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert "Attaching pdb to actor: ('breakpoint_forever'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_pause_msg, "('breakpoint_forever'"]
|
||||||
|
)
|
||||||
|
|
||||||
# do some "next" commands to demonstrate recurrent breakpoint
|
# do some "next" commands to demonstrate recurrent breakpoint
|
||||||
# entries
|
# entries
|
||||||
|
@ -396,7 +448,10 @@ def test_subactor_breakpoint(
|
||||||
child.sendline('continue')
|
child.sendline('continue')
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert "Attaching pdb to actor: ('breakpoint_forever'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_pause_msg, "('breakpoint_forever'"]
|
||||||
|
)
|
||||||
|
|
||||||
if ctlc:
|
if ctlc:
|
||||||
do_ctlc(child)
|
do_ctlc(child)
|
||||||
|
@ -441,7 +496,10 @@ def test_multi_subactors(
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
|
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert "Attaching pdb to actor: ('breakpoint_forever'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_pause_msg, "('breakpoint_forever'"]
|
||||||
|
)
|
||||||
|
|
||||||
if ctlc:
|
if ctlc:
|
||||||
do_ctlc(child)
|
do_ctlc(child)
|
||||||
|
@ -461,7 +519,10 @@ def test_multi_subactors(
|
||||||
# first name_error failure
|
# first name_error failure
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert "Attaching to pdb in crashed actor: ('name_error'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_crash_msg, "('name_error'"]
|
||||||
|
)
|
||||||
assert "NameError" in before
|
assert "NameError" in before
|
||||||
|
|
||||||
if ctlc:
|
if ctlc:
|
||||||
|
@ -487,7 +548,10 @@ def test_multi_subactors(
|
||||||
child.sendline('c')
|
child.sendline('c')
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert "Attaching pdb to actor: ('breakpoint_forever'" in before
|
assert in_prompt_msg(
|
||||||
|
before,
|
||||||
|
[_pause_msg, "('breakpoint_forever'"]
|
||||||
|
)
|
||||||
|
|
||||||
if ctlc:
|
if ctlc:
|
||||||
do_ctlc(child)
|
do_ctlc(child)
|
||||||
|
@ -527,17 +591,21 @@ def test_multi_subactors(
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
|
|
||||||
assert_before(child, [
|
assert_before(
|
||||||
# debugger attaches to root
|
child, [
|
||||||
"Attaching to pdb in crashed actor: ('root'",
|
# debugger attaches to root
|
||||||
|
# "Attaching to pdb in crashed actor: ('root'",
|
||||||
|
_crash_msg,
|
||||||
|
"('root'",
|
||||||
|
|
||||||
# expect a multierror with exceptions for each sub-actor
|
# expect a multierror with exceptions for each sub-actor
|
||||||
"RemoteActorError: ('breakpoint_forever'",
|
"RemoteActorError: ('breakpoint_forever'",
|
||||||
"RemoteActorError: ('name_error'",
|
"RemoteActorError: ('name_error'",
|
||||||
"RemoteActorError: ('spawn_error'",
|
"RemoteActorError: ('spawn_error'",
|
||||||
"RemoteActorError: ('name_error_1'",
|
"RemoteActorError: ('name_error_1'",
|
||||||
'bdb.BdbQuit',
|
'bdb.BdbQuit',
|
||||||
])
|
]
|
||||||
|
)
|
||||||
|
|
||||||
if ctlc:
|
if ctlc:
|
||||||
do_ctlc(child)
|
do_ctlc(child)
|
||||||
|
@ -574,15 +642,22 @@ def test_multi_daemon_subactors(
|
||||||
# the root's tty lock first so anticipate either crash
|
# the root's tty lock first so anticipate either crash
|
||||||
# message on the first entry.
|
# message on the first entry.
|
||||||
|
|
||||||
bp_forever_msg = "Attaching pdb to actor: ('bp_forever'"
|
bp_forev_parts = [_pause_msg, "('bp_forever'"]
|
||||||
|
bp_forev_in_msg = partial(
|
||||||
|
in_prompt_msg,
|
||||||
|
parts=bp_forev_parts,
|
||||||
|
)
|
||||||
|
|
||||||
name_error_msg = "NameError: name 'doggypants' is not defined"
|
name_error_msg = "NameError: name 'doggypants' is not defined"
|
||||||
|
name_error_parts = [name_error_msg]
|
||||||
|
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
if bp_forever_msg in before:
|
|
||||||
next_msg = name_error_msg
|
if bp_forev_in_msg(prompt=before):
|
||||||
|
next_parts = name_error_parts
|
||||||
|
|
||||||
elif name_error_msg in before:
|
elif name_error_msg in before:
|
||||||
next_msg = bp_forever_msg
|
next_parts = bp_forev_parts
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Neither log msg was found !?")
|
raise ValueError("Neither log msg was found !?")
|
||||||
|
@ -599,7 +674,10 @@ def test_multi_daemon_subactors(
|
||||||
|
|
||||||
child.sendline('c')
|
child.sendline('c')
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
assert_before(child, [next_msg])
|
assert_before(
|
||||||
|
child,
|
||||||
|
next_parts,
|
||||||
|
)
|
||||||
|
|
||||||
# XXX: hooray the root clobbering the child here was fixed!
|
# XXX: hooray the root clobbering the child here was fixed!
|
||||||
# IMO, this demonstrates the true power of SC system design.
|
# IMO, this demonstrates the true power of SC system design.
|
||||||
|
@ -607,7 +685,7 @@ def test_multi_daemon_subactors(
|
||||||
# now the root actor won't clobber the bp_forever child
|
# now the root actor won't clobber the bp_forever child
|
||||||
# during it's first access to the debug lock, but will instead
|
# during it's first access to the debug lock, but will instead
|
||||||
# wait for the lock to release, by the edge triggered
|
# wait for the lock to release, by the edge triggered
|
||||||
# ``_debug.Lock.no_remote_has_tty`` event before sending cancel messages
|
# ``devx._debug.Lock.no_remote_has_tty`` event before sending cancel messages
|
||||||
# (via portals) to its underlings B)
|
# (via portals) to its underlings B)
|
||||||
|
|
||||||
# at some point here there should have been some warning msg from
|
# at some point here there should have been some warning msg from
|
||||||
|
@ -623,9 +701,15 @@ def test_multi_daemon_subactors(
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
assert_before(child, [bp_forever_msg])
|
assert_before(
|
||||||
|
child,
|
||||||
|
bp_forev_parts,
|
||||||
|
)
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
assert_before(child, [name_error_msg])
|
assert_before(
|
||||||
|
child,
|
||||||
|
name_error_parts,
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if ctlc:
|
if ctlc:
|
||||||
|
@ -637,7 +721,10 @@ def test_multi_daemon_subactors(
|
||||||
|
|
||||||
child.sendline('c')
|
child.sendline('c')
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
assert_before(child, [name_error_msg])
|
assert_before(
|
||||||
|
child,
|
||||||
|
name_error_parts,
|
||||||
|
)
|
||||||
|
|
||||||
# wait for final error in root
|
# wait for final error in root
|
||||||
# where it crashs with boxed error
|
# where it crashs with boxed error
|
||||||
|
@ -647,7 +734,7 @@ def test_multi_daemon_subactors(
|
||||||
child.expect(PROMPT)
|
child.expect(PROMPT)
|
||||||
assert_before(
|
assert_before(
|
||||||
child,
|
child,
|
||||||
[bp_forever_msg]
|
bp_forev_parts
|
||||||
)
|
)
|
||||||
except AssertionError:
|
except AssertionError:
|
||||||
break
|
break
|
||||||
|
@ -656,7 +743,9 @@ def test_multi_daemon_subactors(
|
||||||
child,
|
child,
|
||||||
[
|
[
|
||||||
# boxed error raised in root task
|
# boxed error raised in root task
|
||||||
"Attaching to pdb in crashed actor: ('root'",
|
# "Attaching to pdb in crashed actor: ('root'",
|
||||||
|
_crash_msg,
|
||||||
|
"('root'",
|
||||||
"_exceptions.RemoteActorError: ('name_error'",
|
"_exceptions.RemoteActorError: ('name_error'",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
@ -770,7 +859,7 @@ def test_multi_nested_subactors_error_through_nurseries(
|
||||||
|
|
||||||
child = spawn('multi_nested_subactors_error_up_through_nurseries')
|
child = spawn('multi_nested_subactors_error_up_through_nurseries')
|
||||||
|
|
||||||
timed_out_early: bool = False
|
# timed_out_early: bool = False
|
||||||
|
|
||||||
for send_char in itertools.cycle(['c', 'q']):
|
for send_char in itertools.cycle(['c', 'q']):
|
||||||
try:
|
try:
|
||||||
|
@ -871,11 +960,14 @@ def test_root_nursery_cancels_before_child_releases_tty_lock(
|
||||||
|
|
||||||
if not timed_out_early:
|
if not timed_out_early:
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert_before(child, [
|
assert_before(
|
||||||
"tractor._exceptions.RemoteActorError: ('spawner0'",
|
child,
|
||||||
"tractor._exceptions.RemoteActorError: ('name_error'",
|
[
|
||||||
"NameError: name 'doggypants' is not defined",
|
"tractor._exceptions.RemoteActorError: ('spawner0'",
|
||||||
])
|
"tractor._exceptions.RemoteActorError: ('name_error'",
|
||||||
|
"NameError: name 'doggypants' is not defined",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_root_cancels_child_context_during_startup(
|
def test_root_cancels_child_context_during_startup(
|
||||||
|
@ -909,8 +1001,10 @@ def test_different_debug_mode_per_actor(
|
||||||
|
|
||||||
# only one actor should enter the debugger
|
# only one actor should enter the debugger
|
||||||
before = str(child.before.decode())
|
before = str(child.before.decode())
|
||||||
assert "Attaching to pdb in crashed actor: ('debugged_boi'" in before
|
assert in_prompt_msg(
|
||||||
assert "RuntimeError" in before
|
before,
|
||||||
|
[_crash_msg, "('debugged_boi'", "RuntimeError"],
|
||||||
|
)
|
||||||
|
|
||||||
if ctlc:
|
if ctlc:
|
||||||
do_ctlc(child)
|
do_ctlc(child)
|
||||||
|
|
|
@ -20,7 +20,7 @@ from tractor._testing import (
|
||||||
def run_example_in_subproc(
|
def run_example_in_subproc(
|
||||||
loglevel: str,
|
loglevel: str,
|
||||||
testdir,
|
testdir,
|
||||||
arb_addr: tuple[str, int],
|
reg_addr: tuple[str, int],
|
||||||
):
|
):
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
|
|
@ -939,6 +939,7 @@ async def tell_little_bro(
|
||||||
def test_peer_spawns_and_cancels_service_subactor(
|
def test_peer_spawns_and_cancels_service_subactor(
|
||||||
debug_mode: bool,
|
debug_mode: bool,
|
||||||
raise_client_error: str,
|
raise_client_error: str,
|
||||||
|
reg_addr: tuple[str, int],
|
||||||
):
|
):
|
||||||
# NOTE: this tests for the modden `mod wks open piker` bug
|
# NOTE: this tests for the modden `mod wks open piker` bug
|
||||||
# discovered as part of implementing workspace ctx
|
# discovered as part of implementing workspace ctx
|
||||||
|
@ -956,6 +957,7 @@ def test_peer_spawns_and_cancels_service_subactor(
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
# NOTE: to halt the peer tasks on ctxc, uncomment this.
|
# NOTE: to halt the peer tasks on ctxc, uncomment this.
|
||||||
debug_mode=debug_mode,
|
debug_mode=debug_mode,
|
||||||
|
registry_addrs=[reg_addr],
|
||||||
) as an:
|
) as an:
|
||||||
server: Portal = await an.start_actor(
|
server: Portal = await an.start_actor(
|
||||||
(server_name := 'spawn_server'),
|
(server_name := 'spawn_server'),
|
||||||
|
|
|
@ -15,9 +15,19 @@ async def sleep_back_actor(
|
||||||
func_name,
|
func_name,
|
||||||
func_defined,
|
func_defined,
|
||||||
exposed_mods,
|
exposed_mods,
|
||||||
|
*,
|
||||||
|
reg_addr: tuple,
|
||||||
):
|
):
|
||||||
if actor_name:
|
if actor_name:
|
||||||
async with tractor.find_actor(actor_name) as portal:
|
async with tractor.find_actor(
|
||||||
|
actor_name,
|
||||||
|
# NOTE: must be set manually since
|
||||||
|
# the subactor doesn't have the reg_addr
|
||||||
|
# fixture code run in it!
|
||||||
|
# TODO: maybe we should just set this once in the
|
||||||
|
# _state mod and derive to all children?
|
||||||
|
registry_addrs=[reg_addr],
|
||||||
|
) as portal:
|
||||||
try:
|
try:
|
||||||
await portal.run(__name__, func_name)
|
await portal.run(__name__, func_name)
|
||||||
except tractor.RemoteActorError as err:
|
except tractor.RemoteActorError as err:
|
||||||
|
@ -52,11 +62,17 @@ async def short_sleep():
|
||||||
'fail_on_syntax',
|
'fail_on_syntax',
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_rpc_errors(reg_addr, to_call, testdir):
|
def test_rpc_errors(
|
||||||
"""Test errors when making various RPC requests to an actor
|
reg_addr,
|
||||||
|
to_call,
|
||||||
|
testdir,
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Test errors when making various RPC requests to an actor
|
||||||
that either doesn't have the requested module exposed or doesn't define
|
that either doesn't have the requested module exposed or doesn't define
|
||||||
the named function.
|
the named function.
|
||||||
"""
|
|
||||||
|
'''
|
||||||
exposed_mods, funcname, inside_err = to_call
|
exposed_mods, funcname, inside_err = to_call
|
||||||
subactor_exposed_mods = []
|
subactor_exposed_mods = []
|
||||||
func_defined = globals().get(funcname, False)
|
func_defined = globals().get(funcname, False)
|
||||||
|
@ -84,8 +100,13 @@ def test_rpc_errors(reg_addr, to_call, testdir):
|
||||||
|
|
||||||
# spawn a subactor which calls us back
|
# spawn a subactor which calls us back
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery(
|
||||||
arbiter_addr=reg_addr,
|
registry_addrs=[reg_addr],
|
||||||
enable_modules=exposed_mods.copy(),
|
enable_modules=exposed_mods.copy(),
|
||||||
|
|
||||||
|
# NOTE: will halt test in REPL if uncommented, so only
|
||||||
|
# do that if actually debugging subactor but keep it
|
||||||
|
# disabled for the test.
|
||||||
|
# debug_mode=True,
|
||||||
) as n:
|
) as n:
|
||||||
|
|
||||||
actor = tractor.current_actor()
|
actor = tractor.current_actor()
|
||||||
|
@ -102,6 +123,7 @@ def test_rpc_errors(reg_addr, to_call, testdir):
|
||||||
exposed_mods=exposed_mods,
|
exposed_mods=exposed_mods,
|
||||||
func_defined=True if func_defined else False,
|
func_defined=True if func_defined else False,
|
||||||
enable_modules=subactor_exposed_mods,
|
enable_modules=subactor_exposed_mods,
|
||||||
|
reg_addr=reg_addr,
|
||||||
)
|
)
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
|
|
|
@ -32,8 +32,7 @@ async def spawn(
|
||||||
|
|
||||||
if actor.is_arbiter:
|
if actor.is_arbiter:
|
||||||
|
|
||||||
async with tractor.open_nursery(
|
async with tractor.open_nursery() as nursery:
|
||||||
) as nursery:
|
|
||||||
|
|
||||||
# forks here
|
# forks here
|
||||||
portal = await nursery.run_in_actor(
|
portal = await nursery.run_in_actor(
|
||||||
|
@ -55,7 +54,9 @@ async def spawn(
|
||||||
return 10
|
return 10
|
||||||
|
|
||||||
|
|
||||||
def test_local_arbiter_subactor_global_state(reg_addr):
|
def test_local_arbiter_subactor_global_state(
|
||||||
|
reg_addr,
|
||||||
|
):
|
||||||
result = trio.run(
|
result = trio.run(
|
||||||
spawn,
|
spawn,
|
||||||
True,
|
True,
|
||||||
|
|
|
@ -18,71 +18,49 @@
|
||||||
tractor: structured concurrent ``trio``-"actors".
|
tractor: structured concurrent ``trio``-"actors".
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from exceptiongroup import BaseExceptionGroup
|
from exceptiongroup import BaseExceptionGroup as BaseExceptionGroup
|
||||||
|
|
||||||
from ._clustering import open_actor_cluster
|
from ._clustering import (
|
||||||
from ._ipc import Channel
|
open_actor_cluster as open_actor_cluster,
|
||||||
|
)
|
||||||
from ._context import (
|
from ._context import (
|
||||||
Context, # the type
|
Context as Context, # the type
|
||||||
context, # a func-decorator
|
context as context, # a func-decorator
|
||||||
)
|
)
|
||||||
from ._streaming import (
|
from ._streaming import (
|
||||||
MsgStream,
|
MsgStream as MsgStream,
|
||||||
stream,
|
stream as stream,
|
||||||
)
|
)
|
||||||
from ._discovery import (
|
from ._discovery import (
|
||||||
get_arbiter,
|
get_arbiter as get_arbiter,
|
||||||
find_actor,
|
find_actor as find_actor,
|
||||||
wait_for_actor,
|
wait_for_actor as wait_for_actor,
|
||||||
query_actor,
|
query_actor as query_actor,
|
||||||
|
)
|
||||||
|
from ._supervise import (
|
||||||
|
open_nursery as open_nursery,
|
||||||
|
ActorNursery as ActorNursery,
|
||||||
)
|
)
|
||||||
from ._supervise import open_nursery
|
|
||||||
from ._state import (
|
from ._state import (
|
||||||
current_actor,
|
current_actor as current_actor,
|
||||||
is_root_process,
|
is_root_process as is_root_process,
|
||||||
)
|
)
|
||||||
from ._exceptions import (
|
from ._exceptions import (
|
||||||
RemoteActorError,
|
RemoteActorError as RemoteActorError,
|
||||||
ModuleNotExposed,
|
ModuleNotExposed as ModuleNotExposed,
|
||||||
ContextCancelled,
|
ContextCancelled as ContextCancelled,
|
||||||
)
|
)
|
||||||
from ._debug import (
|
from .devx import (
|
||||||
breakpoint,
|
breakpoint as breakpoint,
|
||||||
post_mortem,
|
pause as pause,
|
||||||
|
pause_from_sync as pause_from_sync,
|
||||||
|
post_mortem as post_mortem,
|
||||||
)
|
)
|
||||||
from . import msg
|
from . import msg as msg
|
||||||
from ._root import (
|
from ._root import (
|
||||||
run_daemon,
|
run_daemon as run_daemon,
|
||||||
open_root_actor,
|
open_root_actor as open_root_actor,
|
||||||
)
|
)
|
||||||
from ._portal import Portal
|
from ._ipc import Channel as Channel
|
||||||
from ._runtime import Actor
|
from ._portal import Portal as Portal
|
||||||
|
from ._runtime import Actor as Actor
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'Actor',
|
|
||||||
'Channel',
|
|
||||||
'Context',
|
|
||||||
'ContextCancelled',
|
|
||||||
'ModuleNotExposed',
|
|
||||||
'MsgStream',
|
|
||||||
'BaseExceptionGroup',
|
|
||||||
'Portal',
|
|
||||||
'RemoteActorError',
|
|
||||||
'breakpoint',
|
|
||||||
'context',
|
|
||||||
'current_actor',
|
|
||||||
'find_actor',
|
|
||||||
'get_arbiter',
|
|
||||||
'is_root_process',
|
|
||||||
'msg',
|
|
||||||
'open_actor_cluster',
|
|
||||||
'open_nursery',
|
|
||||||
'open_root_actor',
|
|
||||||
'post_mortem',
|
|
||||||
'query_actor',
|
|
||||||
'run_daemon',
|
|
||||||
'stream',
|
|
||||||
'to_asyncio',
|
|
||||||
'wait_for_actor',
|
|
||||||
]
|
|
||||||
|
|
|
@ -868,6 +868,9 @@ class Context:
|
||||||
|
|
||||||
# TODO: maybe we should also call `._res_scope.cancel()` if it
|
# TODO: maybe we should also call `._res_scope.cancel()` if it
|
||||||
# exists to support cancelling any drain loop hangs?
|
# exists to support cancelling any drain loop hangs?
|
||||||
|
# NOTE: this usage actually works here B)
|
||||||
|
# from .devx._debug import breakpoint
|
||||||
|
# await breakpoint()
|
||||||
|
|
||||||
# TODO: add to `Channel`?
|
# TODO: add to `Channel`?
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -15,32 +15,45 @@
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Actor discovery API.
|
Discovery (protocols) API for automatic addressing and location
|
||||||
|
management of (service) actors.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
from typing import (
|
from typing import (
|
||||||
Optional,
|
|
||||||
Union,
|
|
||||||
AsyncGenerator,
|
AsyncGenerator,
|
||||||
|
AsyncContextManager,
|
||||||
|
TYPE_CHECKING,
|
||||||
)
|
)
|
||||||
from contextlib import asynccontextmanager as acm
|
from contextlib import asynccontextmanager as acm
|
||||||
|
import warnings
|
||||||
|
|
||||||
|
from .trionics import gather_contexts
|
||||||
from ._ipc import _connect_chan, Channel
|
from ._ipc import _connect_chan, Channel
|
||||||
from ._portal import (
|
from ._portal import (
|
||||||
Portal,
|
Portal,
|
||||||
open_portal,
|
open_portal,
|
||||||
LocalPortal,
|
LocalPortal,
|
||||||
)
|
)
|
||||||
from ._state import current_actor, _runtime_vars
|
from ._state import (
|
||||||
|
current_actor,
|
||||||
|
_runtime_vars,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from ._runtime import Actor
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def get_arbiter(
|
async def get_registry(
|
||||||
|
|
||||||
host: str,
|
host: str,
|
||||||
port: int,
|
port: int,
|
||||||
|
|
||||||
) -> AsyncGenerator[Union[Portal, LocalPortal], None]:
|
) -> AsyncGenerator[
|
||||||
|
Portal | LocalPortal | None,
|
||||||
|
None,
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Return a portal instance connected to a local or remote
|
Return a portal instance connected to a local or remote
|
||||||
arbiter.
|
arbiter.
|
||||||
|
@ -51,16 +64,33 @@ async def get_arbiter(
|
||||||
if not actor:
|
if not actor:
|
||||||
raise RuntimeError("No actor instance has been defined yet?")
|
raise RuntimeError("No actor instance has been defined yet?")
|
||||||
|
|
||||||
if actor.is_arbiter:
|
if actor.is_registrar:
|
||||||
# we're already the arbiter
|
# we're already the arbiter
|
||||||
# (likely a re-entrant call from the arbiter actor)
|
# (likely a re-entrant call from the arbiter actor)
|
||||||
yield LocalPortal(actor, Channel((host, port)))
|
yield LocalPortal(
|
||||||
|
actor,
|
||||||
|
Channel((host, port))
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
async with _connect_chan(host, port) as chan:
|
async with (
|
||||||
|
_connect_chan(host, port) as chan,
|
||||||
|
open_portal(chan) as regstr_ptl,
|
||||||
|
):
|
||||||
|
yield regstr_ptl
|
||||||
|
|
||||||
async with open_portal(chan) as arb_portal:
|
|
||||||
|
|
||||||
yield arb_portal
|
|
||||||
|
# TODO: deprecate and this remove _arbiter form!
|
||||||
|
@acm
|
||||||
|
async def get_arbiter(*args, **kwargs):
|
||||||
|
warnings.warn(
|
||||||
|
'`tractor.get_arbiter()` is now deprecated!\n'
|
||||||
|
'Use `.get_registry()` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
async with get_registry(*args, **kwargs) as to_yield:
|
||||||
|
yield to_yield
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
|
@ -68,51 +98,80 @@ async def get_root(
|
||||||
**kwargs,
|
**kwargs,
|
||||||
) -> AsyncGenerator[Portal, None]:
|
) -> AsyncGenerator[Portal, None]:
|
||||||
|
|
||||||
|
# TODO: rename mailbox to `_root_maddr` when we finally
|
||||||
|
# add and impl libp2p multi-addrs?
|
||||||
host, port = _runtime_vars['_root_mailbox']
|
host, port = _runtime_vars['_root_mailbox']
|
||||||
assert host is not None
|
assert host is not None
|
||||||
|
|
||||||
async with _connect_chan(host, port) as chan:
|
async with (
|
||||||
async with open_portal(chan, **kwargs) as portal:
|
_connect_chan(host, port) as chan,
|
||||||
yield portal
|
open_portal(chan, **kwargs) as portal,
|
||||||
|
):
|
||||||
|
yield portal
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def query_actor(
|
async def query_actor(
|
||||||
name: str,
|
name: str,
|
||||||
arbiter_sockaddr: Optional[tuple[str, int]] = None,
|
arbiter_sockaddr: tuple[str, int] | None = None,
|
||||||
|
regaddr: tuple[str, int] | None = None,
|
||||||
|
|
||||||
) -> AsyncGenerator[tuple[str, int], None]:
|
) -> AsyncGenerator[
|
||||||
|
tuple[str, int] | None,
|
||||||
|
None,
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Simple address lookup for a given actor name.
|
Make a transport address lookup for an actor name to a specific
|
||||||
|
registrar.
|
||||||
|
|
||||||
Returns the (socket) address or ``None``.
|
Returns the (socket) address or ``None`` if no entry under that
|
||||||
|
name exists for the given registrar listening @ `regaddr`.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
actor = current_actor()
|
actor: Actor = current_actor()
|
||||||
async with get_arbiter(
|
if (
|
||||||
*arbiter_sockaddr or actor._arb_addr
|
name == 'registrar'
|
||||||
) as arb_portal:
|
and actor.is_registrar
|
||||||
|
):
|
||||||
|
raise RuntimeError(
|
||||||
|
'The current actor IS the registry!?'
|
||||||
|
)
|
||||||
|
|
||||||
sockaddr = await arb_portal.run_from_ns(
|
if arbiter_sockaddr is not None:
|
||||||
|
warnings.warn(
|
||||||
|
'`tractor.query_actor(regaddr=<blah>)` is deprecated.\n'
|
||||||
|
'Use `registry_addrs: list[tuple]` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
regaddr: list[tuple[str, int]] = arbiter_sockaddr
|
||||||
|
|
||||||
|
reg_portal: Portal
|
||||||
|
regaddr: tuple[str, int] = regaddr or actor.reg_addrs[0]
|
||||||
|
async with get_registry(*regaddr) as reg_portal:
|
||||||
|
# TODO: return portals to all available actors - for now
|
||||||
|
# just the last one that registered
|
||||||
|
sockaddr: tuple[str, int] = await reg_portal.run_from_ns(
|
||||||
'self',
|
'self',
|
||||||
'find_actor',
|
'find_actor',
|
||||||
name=name,
|
name=name,
|
||||||
)
|
)
|
||||||
|
yield sockaddr
|
||||||
# TODO: return portals to all available actors - for now just
|
|
||||||
# the last one that registered
|
|
||||||
if name == 'arbiter' and actor.is_arbiter:
|
|
||||||
raise RuntimeError("The current actor is the arbiter")
|
|
||||||
|
|
||||||
yield sockaddr if sockaddr else None
|
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def find_actor(
|
async def find_actor(
|
||||||
name: str,
|
name: str,
|
||||||
arbiter_sockaddr: tuple[str, int] | None = None
|
arbiter_sockaddr: tuple[str, int]|None = None,
|
||||||
|
registry_addrs: list[tuple[str, int]]|None = None,
|
||||||
|
|
||||||
) -> AsyncGenerator[Optional[Portal], None]:
|
only_first: bool = True,
|
||||||
|
raise_on_none: bool = False,
|
||||||
|
|
||||||
|
) -> AsyncGenerator[
|
||||||
|
Portal | list[Portal] | None,
|
||||||
|
None,
|
||||||
|
]:
|
||||||
'''
|
'''
|
||||||
Ask the arbiter to find actor(s) by name.
|
Ask the arbiter to find actor(s) by name.
|
||||||
|
|
||||||
|
@ -120,24 +179,83 @@ async def find_actor(
|
||||||
known to the arbiter.
|
known to the arbiter.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
async with query_actor(
|
if arbiter_sockaddr is not None:
|
||||||
name=name,
|
warnings.warn(
|
||||||
arbiter_sockaddr=arbiter_sockaddr,
|
'`tractor.find_actor(arbiter_sockaddr=<blah>)` is deprecated.\n'
|
||||||
) as sockaddr:
|
'Use `registry_addrs: list[tuple]` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
registry_addrs: list[tuple[str, int]] = [arbiter_sockaddr]
|
||||||
|
|
||||||
if sockaddr:
|
@acm
|
||||||
async with _connect_chan(*sockaddr) as chan:
|
async def maybe_open_portal_from_reg_addr(
|
||||||
async with open_portal(chan) as portal:
|
addr: tuple[str, int],
|
||||||
yield portal
|
):
|
||||||
else:
|
async with query_actor(
|
||||||
|
name=name,
|
||||||
|
regaddr=addr,
|
||||||
|
) as sockaddr:
|
||||||
|
if sockaddr:
|
||||||
|
async with _connect_chan(*sockaddr) as chan:
|
||||||
|
async with open_portal(chan) as portal:
|
||||||
|
yield portal
|
||||||
|
else:
|
||||||
|
yield None
|
||||||
|
|
||||||
|
if not registry_addrs:
|
||||||
|
# XXX NOTE: make sure to dynamically read the value on
|
||||||
|
# every call since something may change it globally (eg.
|
||||||
|
# like in our discovery test suite)!
|
||||||
|
from . import _root
|
||||||
|
registry_addrs = (
|
||||||
|
_runtime_vars['_registry_addrs']
|
||||||
|
or
|
||||||
|
_root._default_lo_addrs
|
||||||
|
)
|
||||||
|
|
||||||
|
maybe_portals: list[
|
||||||
|
AsyncContextManager[tuple[str, int]]
|
||||||
|
] = list(
|
||||||
|
maybe_open_portal_from_reg_addr(addr)
|
||||||
|
for addr in registry_addrs
|
||||||
|
)
|
||||||
|
|
||||||
|
async with gather_contexts(
|
||||||
|
mngrs=maybe_portals,
|
||||||
|
) as portals:
|
||||||
|
# log.runtime(
|
||||||
|
# 'Gathered portals:\n'
|
||||||
|
# f'{portals}'
|
||||||
|
# )
|
||||||
|
# NOTE: `gather_contexts()` will return a
|
||||||
|
# `tuple[None, None, ..., None]` if no contact
|
||||||
|
# can be made with any regstrar at any of the
|
||||||
|
# N provided addrs!
|
||||||
|
if not any(portals):
|
||||||
|
if raise_on_none:
|
||||||
|
raise RuntimeError(
|
||||||
|
f'No actor "{name}" found registered @ {registry_addrs}'
|
||||||
|
)
|
||||||
yield None
|
yield None
|
||||||
|
return
|
||||||
|
|
||||||
|
portals: list[Portal] = list(portals)
|
||||||
|
if only_first:
|
||||||
|
yield portals[0]
|
||||||
|
|
||||||
|
else:
|
||||||
|
# TODO: currently this may return multiple portals
|
||||||
|
# given there are multi-homed or multiple registrars..
|
||||||
|
# SO, we probably need de-duplication logic?
|
||||||
|
yield portals
|
||||||
|
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def wait_for_actor(
|
async def wait_for_actor(
|
||||||
name: str,
|
name: str,
|
||||||
arbiter_sockaddr: tuple[str, int] | None = None,
|
arbiter_sockaddr: tuple[str, int] | None = None,
|
||||||
# registry_addr: tuple[str, int] | None = None,
|
registry_addr: tuple[str, int] | None = None,
|
||||||
|
|
||||||
) -> AsyncGenerator[Portal, None]:
|
) -> AsyncGenerator[Portal, None]:
|
||||||
'''
|
'''
|
||||||
|
@ -146,17 +264,31 @@ async def wait_for_actor(
|
||||||
A portal to the first registered actor is returned.
|
A portal to the first registered actor is returned.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
actor = current_actor()
|
actor: Actor = current_actor()
|
||||||
|
|
||||||
async with get_arbiter(
|
if arbiter_sockaddr is not None:
|
||||||
*arbiter_sockaddr or actor._arb_addr,
|
warnings.warn(
|
||||||
) as arb_portal:
|
'`tractor.wait_for_actor(arbiter_sockaddr=<foo>)` is deprecated.\n'
|
||||||
sockaddrs = await arb_portal.run_from_ns(
|
'Use `registry_addr: tuple` instead!',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
registry_addr: tuple[str, int] = arbiter_sockaddr
|
||||||
|
|
||||||
|
# TODO: use `.trionics.gather_contexts()` like
|
||||||
|
# above in `find_actor()` as well?
|
||||||
|
reg_portal: Portal
|
||||||
|
regaddr: tuple[str, int] = registry_addr or actor.reg_addrs[0]
|
||||||
|
async with get_registry(*regaddr) as reg_portal:
|
||||||
|
sockaddrs = await reg_portal.run_from_ns(
|
||||||
'self',
|
'self',
|
||||||
'wait_for_actor',
|
'wait_for_actor',
|
||||||
name=name,
|
name=name,
|
||||||
)
|
)
|
||||||
sockaddr = sockaddrs[-1]
|
|
||||||
|
# get latest registered addr by default?
|
||||||
|
# TODO: offer multi-portal yields in multi-homed case?
|
||||||
|
sockaddr: tuple[str, int] = sockaddrs[-1]
|
||||||
|
|
||||||
async with _connect_chan(*sockaddr) as chan:
|
async with _connect_chan(*sockaddr) as chan:
|
||||||
async with open_portal(chan) as portal:
|
async with open_portal(chan) as portal:
|
||||||
|
|
|
@ -47,8 +47,8 @@ log = get_logger(__name__)
|
||||||
|
|
||||||
def _mp_main(
|
def _mp_main(
|
||||||
|
|
||||||
actor: Actor, # type: ignore
|
actor: Actor,
|
||||||
accept_addr: tuple[str, int],
|
accept_addrs: list[tuple[str, int]],
|
||||||
forkserver_info: tuple[Any, Any, Any, Any, Any],
|
forkserver_info: tuple[Any, Any, Any, Any, Any],
|
||||||
start_method: SpawnMethodKey,
|
start_method: SpawnMethodKey,
|
||||||
parent_addr: tuple[str, int] | None = None,
|
parent_addr: tuple[str, int] | None = None,
|
||||||
|
@ -77,8 +77,8 @@ def _mp_main(
|
||||||
log.debug(f"parent_addr is {parent_addr}")
|
log.debug(f"parent_addr is {parent_addr}")
|
||||||
trio_main = partial(
|
trio_main = partial(
|
||||||
async_main,
|
async_main,
|
||||||
actor,
|
actor=actor,
|
||||||
accept_addr,
|
accept_addrs=accept_addrs,
|
||||||
parent_addr=parent_addr
|
parent_addr=parent_addr
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
|
@ -96,7 +96,7 @@ def _mp_main(
|
||||||
|
|
||||||
def _trio_main(
|
def _trio_main(
|
||||||
|
|
||||||
actor: Actor, # type: ignore
|
actor: Actor,
|
||||||
*,
|
*,
|
||||||
parent_addr: tuple[str, int] | None = None,
|
parent_addr: tuple[str, int] | None = None,
|
||||||
infect_asyncio: bool = False,
|
infect_asyncio: bool = False,
|
||||||
|
|
|
@ -517,7 +517,9 @@ class Channel:
|
||||||
|
|
||||||
@acm
|
@acm
|
||||||
async def _connect_chan(
|
async def _connect_chan(
|
||||||
host: str, port: int
|
host: str,
|
||||||
|
port: int
|
||||||
|
|
||||||
) -> typing.AsyncGenerator[Channel, None]:
|
) -> typing.AsyncGenerator[Channel, None]:
|
||||||
'''
|
'''
|
||||||
Create and connect a channel with disconnect on context manager
|
Create and connect a channel with disconnect on context manager
|
||||||
|
|
|
@ -0,0 +1,151 @@
|
||||||
|
# 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/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
Multiaddress parser and utils according the spec(s) defined by
|
||||||
|
`libp2p` and used in dependent project such as `ipfs`:
|
||||||
|
|
||||||
|
- https://docs.libp2p.io/concepts/fundamentals/addressing/
|
||||||
|
- https://github.com/libp2p/specs/blob/master/addressing/README.md
|
||||||
|
|
||||||
|
'''
|
||||||
|
from typing import Iterator
|
||||||
|
|
||||||
|
from bidict import bidict
|
||||||
|
|
||||||
|
# TODO: see if we can leverage libp2p ecosys projects instead of
|
||||||
|
# rolling our own (parser) impls of the above addressing specs:
|
||||||
|
# - https://github.com/libp2p/py-libp2p
|
||||||
|
# - https://docs.libp2p.io/concepts/nat/circuit-relay/#relay-addresses
|
||||||
|
# prots: bidict[int, str] = bidict({
|
||||||
|
prots: bidict[int, str] = {
|
||||||
|
'ipv4': 3,
|
||||||
|
'ipv6': 3,
|
||||||
|
'wg': 3,
|
||||||
|
|
||||||
|
'tcp': 4,
|
||||||
|
'udp': 4,
|
||||||
|
|
||||||
|
# TODO: support the next-gen shite Bo
|
||||||
|
# 'quic': 4,
|
||||||
|
# 'ssh': 7, # via rsyscall bootstrapping
|
||||||
|
}
|
||||||
|
|
||||||
|
prot_params: dict[str, tuple[str]] = {
|
||||||
|
'ipv4': ('addr',),
|
||||||
|
'ipv6': ('addr',),
|
||||||
|
'wg': ('addr', 'port', 'pubkey'),
|
||||||
|
|
||||||
|
'tcp': ('port',),
|
||||||
|
'udp': ('port',),
|
||||||
|
|
||||||
|
# 'quic': ('port',),
|
||||||
|
# 'ssh': ('port',),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def iter_prot_layers(
|
||||||
|
multiaddr: str,
|
||||||
|
) -> Iterator[
|
||||||
|
tuple[
|
||||||
|
int,
|
||||||
|
list[str]
|
||||||
|
]
|
||||||
|
]:
|
||||||
|
'''
|
||||||
|
Unpack a libp2p style "multiaddress" into multiple "segments"
|
||||||
|
for each "layer" of the protocoll stack (in OSI terms).
|
||||||
|
|
||||||
|
'''
|
||||||
|
tokens: list[str] = multiaddr.split('/')
|
||||||
|
root, tokens = tokens[0], tokens[1:]
|
||||||
|
assert not root # there is a root '/' on LHS
|
||||||
|
itokens = iter(tokens)
|
||||||
|
|
||||||
|
prot: str | None = None
|
||||||
|
params: list[str] = []
|
||||||
|
for token in itokens:
|
||||||
|
# every prot path should start with a known
|
||||||
|
# key-str.
|
||||||
|
if token in prots:
|
||||||
|
if prot is None:
|
||||||
|
prot: str = token
|
||||||
|
else:
|
||||||
|
yield prot, params
|
||||||
|
prot = token
|
||||||
|
|
||||||
|
params = []
|
||||||
|
|
||||||
|
elif token not in prots:
|
||||||
|
params.append(token)
|
||||||
|
|
||||||
|
else:
|
||||||
|
yield prot, params
|
||||||
|
|
||||||
|
|
||||||
|
def parse_maddr(
|
||||||
|
multiaddr: str,
|
||||||
|
) -> dict[str, str | int | dict]:
|
||||||
|
'''
|
||||||
|
Parse a libp2p style "multiaddress" into its distinct protocol
|
||||||
|
segments where each segment is of the form:
|
||||||
|
|
||||||
|
`../<protocol>/<param0>/<param1>/../<paramN>`
|
||||||
|
|
||||||
|
and is loaded into a (order preserving) `layers: dict[str,
|
||||||
|
dict[str, Any]` which holds each protocol-layer-segment of the
|
||||||
|
original `str` path as a separate entry according to its approx
|
||||||
|
OSI "layer number".
|
||||||
|
|
||||||
|
Any `paramN` in the path must be distinctly defined by a str-token in the
|
||||||
|
(module global) `prot_params` table.
|
||||||
|
|
||||||
|
For eg. for wireguard which requires an address, port number and publickey
|
||||||
|
the protocol params are specified as the entry:
|
||||||
|
|
||||||
|
'wg': ('addr', 'port', 'pubkey'),
|
||||||
|
|
||||||
|
and are thus parsed from a maddr in that order:
|
||||||
|
`'/wg/1.1.1.1/51820/<pubkey>'`
|
||||||
|
|
||||||
|
'''
|
||||||
|
layers: dict[str, str | int | dict] = {}
|
||||||
|
for (
|
||||||
|
prot_key,
|
||||||
|
params,
|
||||||
|
) in iter_prot_layers(multiaddr):
|
||||||
|
|
||||||
|
layer: int = prots[prot_key] # OSI layer used for sorting
|
||||||
|
ep: dict[str, int | str] = {'layer': layer}
|
||||||
|
layers[prot_key] = ep
|
||||||
|
|
||||||
|
# TODO; validation and resolving of names:
|
||||||
|
# - each param via a validator provided as part of the
|
||||||
|
# prot_params def? (also see `"port"` case below..)
|
||||||
|
# - do a resolv step that will check addrs against
|
||||||
|
# any loaded network.resolv: dict[str, str]
|
||||||
|
rparams: list = list(reversed(params))
|
||||||
|
for key in prot_params[prot_key]:
|
||||||
|
val: str | int = rparams.pop()
|
||||||
|
|
||||||
|
# TODO: UGHH, dunno what we should do for validation
|
||||||
|
# here, put it in the params spec somehow?
|
||||||
|
if key == 'port':
|
||||||
|
val = int(val)
|
||||||
|
|
||||||
|
ep[key] = val
|
||||||
|
|
||||||
|
return layers
|
|
@ -461,7 +461,12 @@ class LocalPortal:
|
||||||
actor: 'Actor' # type: ignore # noqa
|
actor: 'Actor' # type: ignore # noqa
|
||||||
channel: Channel
|
channel: Channel
|
||||||
|
|
||||||
async def run_from_ns(self, ns: str, func_name: str, **kwargs) -> Any:
|
async def run_from_ns(
|
||||||
|
self,
|
||||||
|
ns: str,
|
||||||
|
func_name: str,
|
||||||
|
**kwargs,
|
||||||
|
) -> Any:
|
||||||
'''
|
'''
|
||||||
Run a requested local function from a namespace path and
|
Run a requested local function from a namespace path and
|
||||||
return it's result.
|
return it's result.
|
||||||
|
|
233
tractor/_root.py
233
tractor/_root.py
|
@ -25,7 +25,6 @@ import logging
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import typing
|
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
|
|
||||||
|
@ -38,7 +37,7 @@ from ._runtime import (
|
||||||
# Arbiter as Registry,
|
# Arbiter as Registry,
|
||||||
async_main,
|
async_main,
|
||||||
)
|
)
|
||||||
from . import _debug
|
from .devx import _debug
|
||||||
from . import _spawn
|
from . import _spawn
|
||||||
from . import _state
|
from . import _state
|
||||||
from . import log
|
from . import log
|
||||||
|
@ -47,8 +46,14 @@ from ._exceptions import is_multi_cancelled
|
||||||
|
|
||||||
|
|
||||||
# set at startup and after forks
|
# set at startup and after forks
|
||||||
_default_arbiter_host: str = '127.0.0.1'
|
_default_host: str = '127.0.0.1'
|
||||||
_default_arbiter_port: int = 1616
|
_default_port: int = 1616
|
||||||
|
|
||||||
|
# default registry always on localhost
|
||||||
|
_default_lo_addrs: list[tuple[str, int]] = [(
|
||||||
|
_default_host,
|
||||||
|
_default_port,
|
||||||
|
)]
|
||||||
|
|
||||||
|
|
||||||
logger = log.get_logger('tractor')
|
logger = log.get_logger('tractor')
|
||||||
|
@ -59,28 +64,32 @@ async def open_root_actor(
|
||||||
|
|
||||||
*,
|
*,
|
||||||
# defaults are above
|
# defaults are above
|
||||||
arbiter_addr: tuple[str, int] | None = None,
|
registry_addrs: list[tuple[str, int]]|None = None,
|
||||||
|
|
||||||
# defaults are above
|
# defaults are above
|
||||||
registry_addr: tuple[str, int] | None = None,
|
arbiter_addr: tuple[str, int]|None = None,
|
||||||
|
|
||||||
name: str | None = 'root',
|
name: str|None = 'root',
|
||||||
|
|
||||||
# either the `multiprocessing` start method:
|
# either the `multiprocessing` start method:
|
||||||
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
|
# https://docs.python.org/3/library/multiprocessing.html#contexts-and-start-methods
|
||||||
# OR `trio` (the new default).
|
# OR `trio` (the new default).
|
||||||
start_method: _spawn.SpawnMethodKey | None = None,
|
start_method: _spawn.SpawnMethodKey|None = None,
|
||||||
|
|
||||||
# enables the multi-process debugger support
|
# enables the multi-process debugger support
|
||||||
debug_mode: bool = False,
|
debug_mode: bool = False,
|
||||||
|
|
||||||
# internal logging
|
# internal logging
|
||||||
loglevel: str | None = None,
|
loglevel: str|None = None,
|
||||||
|
|
||||||
enable_modules: list | None = None,
|
enable_modules: list|None = None,
|
||||||
rpc_module_paths: list | None = None,
|
rpc_module_paths: list|None = None,
|
||||||
|
|
||||||
) -> typing.Any:
|
# NOTE: allow caller to ensure that only one registry exists
|
||||||
|
# and that this call creates it.
|
||||||
|
ensure_registry: bool = False,
|
||||||
|
|
||||||
|
) -> Actor:
|
||||||
'''
|
'''
|
||||||
Runtime init entry point for ``tractor``.
|
Runtime init entry point for ``tractor``.
|
||||||
|
|
||||||
|
@ -90,7 +99,7 @@ async def open_root_actor(
|
||||||
# https://github.com/python-trio/trio/issues/1155#issuecomment-742964018
|
# https://github.com/python-trio/trio/issues/1155#issuecomment-742964018
|
||||||
builtin_bp_handler = sys.breakpointhook
|
builtin_bp_handler = sys.breakpointhook
|
||||||
orig_bp_path: str | None = os.environ.get('PYTHONBREAKPOINT', None)
|
orig_bp_path: str | None = os.environ.get('PYTHONBREAKPOINT', None)
|
||||||
os.environ['PYTHONBREAKPOINT'] = 'tractor._debug._set_trace'
|
os.environ['PYTHONBREAKPOINT'] = 'tractor.devx._debug.pause_from_sync'
|
||||||
|
|
||||||
# attempt to retreive ``trio``'s sigint handler and stash it
|
# attempt to retreive ``trio``'s sigint handler and stash it
|
||||||
# on our debugger lock state.
|
# on our debugger lock state.
|
||||||
|
@ -100,7 +109,11 @@ async def open_root_actor(
|
||||||
_state._runtime_vars['_is_root'] = True
|
_state._runtime_vars['_is_root'] = True
|
||||||
|
|
||||||
# caps based rpc list
|
# caps based rpc list
|
||||||
enable_modules = enable_modules or []
|
enable_modules = (
|
||||||
|
enable_modules
|
||||||
|
or
|
||||||
|
[]
|
||||||
|
)
|
||||||
|
|
||||||
if rpc_module_paths:
|
if rpc_module_paths:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
|
@ -116,29 +129,34 @@ async def open_root_actor(
|
||||||
|
|
||||||
if arbiter_addr is not None:
|
if arbiter_addr is not None:
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
'`arbiter_addr` is now deprecated and has been renamed to'
|
'`arbiter_addr` is now deprecated\n'
|
||||||
'`registry_addr`.\nUse that instead..',
|
'Use `registry_addrs: list[tuple]` instead..',
|
||||||
DeprecationWarning,
|
DeprecationWarning,
|
||||||
stacklevel=2,
|
stacklevel=2,
|
||||||
)
|
)
|
||||||
|
registry_addrs = [arbiter_addr]
|
||||||
|
|
||||||
registry_addr = (host, port) = (
|
registry_addrs: list[tuple[str, int]] = (
|
||||||
registry_addr
|
registry_addrs
|
||||||
or arbiter_addr
|
or
|
||||||
or (
|
_default_lo_addrs
|
||||||
_default_arbiter_host,
|
|
||||||
_default_arbiter_port,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
assert registry_addrs
|
||||||
|
|
||||||
loglevel = (loglevel or log._default_loglevel).upper()
|
loglevel = (
|
||||||
|
loglevel
|
||||||
|
or log._default_loglevel
|
||||||
|
).upper()
|
||||||
|
|
||||||
if debug_mode and _spawn._spawn_method == 'trio':
|
if (
|
||||||
|
debug_mode
|
||||||
|
and _spawn._spawn_method == 'trio'
|
||||||
|
):
|
||||||
_state._runtime_vars['_debug_mode'] = True
|
_state._runtime_vars['_debug_mode'] = True
|
||||||
|
|
||||||
# expose internal debug module to every actor allowing
|
# expose internal debug module to every actor allowing for
|
||||||
# for use of ``await tractor.breakpoint()``
|
# use of ``await tractor.pause()``
|
||||||
enable_modules.append('tractor._debug')
|
enable_modules.append('tractor.devx._debug')
|
||||||
|
|
||||||
# if debug mode get's enabled *at least* use that level of
|
# if debug mode get's enabled *at least* use that level of
|
||||||
# logging for some informative console prompts.
|
# logging for some informative console prompts.
|
||||||
|
@ -156,75 +174,146 @@ async def open_root_actor(
|
||||||
"Debug mode is only supported for the `trio` backend!"
|
"Debug mode is only supported for the `trio` backend!"
|
||||||
)
|
)
|
||||||
|
|
||||||
log.get_console_log(loglevel)
|
assert loglevel
|
||||||
|
_log = log.get_console_log(loglevel)
|
||||||
|
assert _log
|
||||||
|
|
||||||
try:
|
# TODO: factor this into `.devx._stackscope`!!
|
||||||
# make a temporary connection to see if an arbiter exists,
|
if debug_mode:
|
||||||
# if one can't be made quickly we assume none exists.
|
try:
|
||||||
arbiter_found = False
|
logger.info('Enabling `stackscope` traces on SIGUSR1')
|
||||||
|
from .devx import enable_stack_on_sig
|
||||||
|
enable_stack_on_sig()
|
||||||
|
except ImportError:
|
||||||
|
logger.warning(
|
||||||
|
'`stackscope` not installed for use in debug mode!'
|
||||||
|
)
|
||||||
|
|
||||||
# TODO: this connect-and-bail forces us to have to carefully
|
# closed into below ping task-func
|
||||||
# rewrap TCP 104-connection-reset errors as EOF so as to avoid
|
ponged_addrs: list[tuple[str, int]] = []
|
||||||
# propagating cancel-causing errors to the channel-msg loop
|
|
||||||
# machinery. Likely it would be better to eventually have
|
|
||||||
# a "discovery" protocol with basic handshake instead.
|
|
||||||
with trio.move_on_after(1):
|
|
||||||
async with _connect_chan(host, port):
|
|
||||||
arbiter_found = True
|
|
||||||
|
|
||||||
except OSError:
|
async def ping_tpt_socket(
|
||||||
# TODO: make this a "discovery" log level?
|
addr: tuple[str, int],
|
||||||
logger.warning(f"No actor registry found @ {host}:{port}")
|
timeout: float = 1,
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Attempt temporary connection to see if a registry is
|
||||||
|
listening at the requested address by a tranport layer
|
||||||
|
ping.
|
||||||
|
|
||||||
# create a local actor and start up its main routine/task
|
If a connection can't be made quickly we assume none no
|
||||||
if arbiter_found:
|
server is listening at that addr.
|
||||||
|
|
||||||
|
'''
|
||||||
|
try:
|
||||||
|
# TODO: this connect-and-bail forces us to have to
|
||||||
|
# carefully rewrap TCP 104-connection-reset errors as
|
||||||
|
# EOF so as to avoid propagating cancel-causing errors
|
||||||
|
# to the channel-msg loop machinery. Likely it would
|
||||||
|
# be better to eventually have a "discovery" protocol
|
||||||
|
# with basic handshake instead?
|
||||||
|
with trio.move_on_after(timeout):
|
||||||
|
async with _connect_chan(*addr):
|
||||||
|
ponged_addrs.append(addr)
|
||||||
|
|
||||||
|
except OSError:
|
||||||
|
# TODO: make this a "discovery" log level?
|
||||||
|
logger.warning(f'No actor registry found @ {addr}')
|
||||||
|
|
||||||
|
async with trio.open_nursery() as tn:
|
||||||
|
for addr in registry_addrs:
|
||||||
|
tn.start_soon(
|
||||||
|
ping_tpt_socket,
|
||||||
|
tuple(addr), # TODO: just drop this requirement?
|
||||||
|
)
|
||||||
|
|
||||||
|
trans_bind_addrs: list[tuple[str, int]] = []
|
||||||
|
|
||||||
|
# Create a new local root-actor instance which IS NOT THE
|
||||||
|
# REGISTRAR
|
||||||
|
if ponged_addrs:
|
||||||
|
|
||||||
|
if ensure_registry:
|
||||||
|
raise RuntimeError(
|
||||||
|
f'Failed to open `{name}`@{ponged_addrs}: '
|
||||||
|
'registry socket(s) already bound'
|
||||||
|
)
|
||||||
|
|
||||||
# we were able to connect to an arbiter
|
# we were able to connect to an arbiter
|
||||||
logger.info(f"Arbiter seems to exist @ {host}:{port}")
|
logger.info(
|
||||||
|
f'Registry(s) seem(s) to exist @ {ponged_addrs}'
|
||||||
|
)
|
||||||
|
|
||||||
actor = Actor(
|
actor = Actor(
|
||||||
name or 'anonymous',
|
name=name or 'anonymous',
|
||||||
arbiter_addr=registry_addr,
|
registry_addrs=ponged_addrs,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
enable_modules=enable_modules,
|
enable_modules=enable_modules,
|
||||||
)
|
)
|
||||||
host, port = (host, 0)
|
# DO NOT use the registry_addrs as the transport server
|
||||||
|
# addrs for this new non-registar, root-actor.
|
||||||
|
for host, port in ponged_addrs:
|
||||||
|
# NOTE: zero triggers dynamic OS port allocation
|
||||||
|
trans_bind_addrs.append((host, 0))
|
||||||
|
|
||||||
|
# Start this local actor as the "registrar", aka a regular
|
||||||
|
# actor who manages the local registry of "mailboxes" of
|
||||||
|
# other process-tree-local sub-actors.
|
||||||
else:
|
else:
|
||||||
# start this local actor as the arbiter (aka a regular actor who
|
|
||||||
# manages the local registry of "mailboxes")
|
|
||||||
|
|
||||||
# Note that if the current actor is the arbiter it is desirable
|
# NOTE that if the current actor IS THE REGISTAR, the
|
||||||
# for it to stay up indefinitely until a re-election process has
|
# following init steps are taken:
|
||||||
# taken place - which is not implemented yet FYI).
|
# - the tranport layer server is bound to each (host, port)
|
||||||
|
# pair defined in provided registry_addrs, or the default.
|
||||||
|
trans_bind_addrs = registry_addrs
|
||||||
|
|
||||||
|
# - it is normally desirable for any registrar to stay up
|
||||||
|
# indefinitely until either all registered (child/sub)
|
||||||
|
# actors are terminated (via SC supervision) or,
|
||||||
|
# a re-election process has taken place.
|
||||||
|
# NOTE: all of ^ which is not implemented yet - see:
|
||||||
|
# https://github.com/goodboy/tractor/issues/216
|
||||||
|
# https://github.com/goodboy/tractor/pull/348
|
||||||
|
# https://github.com/goodboy/tractor/issues/296
|
||||||
|
|
||||||
actor = Arbiter(
|
actor = Arbiter(
|
||||||
name or 'arbiter',
|
name or 'registrar',
|
||||||
arbiter_addr=registry_addr,
|
registry_addrs=registry_addrs,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
enable_modules=enable_modules,
|
enable_modules=enable_modules,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Start up main task set via core actor-runtime nurseries.
|
||||||
try:
|
try:
|
||||||
# assign process-local actor
|
# assign process-local actor
|
||||||
_state._current_actor = actor
|
_state._current_actor = actor
|
||||||
|
|
||||||
# start local channel-server and fake the portal API
|
# start local channel-server and fake the portal API
|
||||||
# NOTE: this won't block since we provide the nursery
|
# NOTE: this won't block since we provide the nursery
|
||||||
logger.info(f"Starting local {actor} @ {host}:{port}")
|
ml_addrs_str: str = '\n'.join(
|
||||||
|
f'@{addr}' for addr in trans_bind_addrs
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
f'Starting local {actor.uid} on the following transport addrs:\n'
|
||||||
|
f'{ml_addrs_str}'
|
||||||
|
)
|
||||||
|
|
||||||
# start the actor runtime in a new task
|
# start the actor runtime in a new task
|
||||||
async with trio.open_nursery() as nursery:
|
async with trio.open_nursery() as nursery:
|
||||||
|
|
||||||
# ``_runtime.async_main()`` creates an internal nursery and
|
# ``_runtime.async_main()`` creates an internal nursery
|
||||||
# thus blocks here until the entire underlying actor tree has
|
# and blocks here until any underlying actor(-process)
|
||||||
# terminated thereby conducting structured concurrency.
|
# tree has terminated thereby conducting so called
|
||||||
|
# "end-to-end" structured concurrency throughout an
|
||||||
|
# entire hierarchical python sub-process set; all
|
||||||
|
# "actor runtime" primitives are SC-compat and thus all
|
||||||
|
# transitively spawned actors/processes must be as
|
||||||
|
# well.
|
||||||
await nursery.start(
|
await nursery.start(
|
||||||
partial(
|
partial(
|
||||||
async_main,
|
async_main,
|
||||||
actor,
|
actor,
|
||||||
accept_addr=(host, port),
|
accept_addrs=trans_bind_addrs,
|
||||||
parent_addr=None
|
parent_addr=None
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -236,15 +325,16 @@ async def open_root_actor(
|
||||||
BaseExceptionGroup,
|
BaseExceptionGroup,
|
||||||
) as err:
|
) as err:
|
||||||
|
|
||||||
entered = await _debug._maybe_enter_pm(err)
|
entered: bool = await _debug._maybe_enter_pm(err)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
not entered
|
not entered
|
||||||
and not is_multi_cancelled(err)
|
and
|
||||||
|
not is_multi_cancelled(err)
|
||||||
):
|
):
|
||||||
logger.exception('Root actor crashed:\n')
|
logger.exception('Root actor crashed:\n')
|
||||||
|
|
||||||
# always re-raise
|
# ALWAYS re-raise any error bubbled up from the
|
||||||
|
# runtime!
|
||||||
raise
|
raise
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
@ -265,7 +355,7 @@ async def open_root_actor(
|
||||||
_state._current_actor = None
|
_state._current_actor = None
|
||||||
_state._last_actor_terminated = actor
|
_state._last_actor_terminated = actor
|
||||||
|
|
||||||
# restore breakpoint hook state
|
# restore built-in `breakpoint()` hook state
|
||||||
sys.breakpointhook = builtin_bp_handler
|
sys.breakpointhook = builtin_bp_handler
|
||||||
if orig_bp_path is not None:
|
if orig_bp_path is not None:
|
||||||
os.environ['PYTHONBREAKPOINT'] = orig_bp_path
|
os.environ['PYTHONBREAKPOINT'] = orig_bp_path
|
||||||
|
@ -281,10 +371,7 @@ def run_daemon(
|
||||||
|
|
||||||
# runtime kwargs
|
# runtime kwargs
|
||||||
name: str | None = 'root',
|
name: str | None = 'root',
|
||||||
registry_addr: tuple[str, int] = (
|
registry_addrs: list[tuple[str, int]] = _default_lo_addrs,
|
||||||
_default_arbiter_host,
|
|
||||||
_default_arbiter_port,
|
|
||||||
),
|
|
||||||
|
|
||||||
start_method: str | None = None,
|
start_method: str | None = None,
|
||||||
debug_mode: bool = False,
|
debug_mode: bool = False,
|
||||||
|
@ -308,7 +395,7 @@ def run_daemon(
|
||||||
async def _main():
|
async def _main():
|
||||||
|
|
||||||
async with open_root_actor(
|
async with open_root_actor(
|
||||||
registry_addr=registry_addr,
|
registry_addrs=registry_addrs,
|
||||||
name=name,
|
name=name,
|
||||||
start_method=start_method,
|
start_method=start_method,
|
||||||
debug_mode=debug_mode,
|
debug_mode=debug_mode,
|
||||||
|
|
|
@ -45,6 +45,7 @@ from functools import partial
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
import importlib
|
import importlib
|
||||||
import importlib.util
|
import importlib.util
|
||||||
|
import os
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
@ -55,7 +56,7 @@ from typing import (
|
||||||
)
|
)
|
||||||
import uuid
|
import uuid
|
||||||
from types import ModuleType
|
from types import ModuleType
|
||||||
import os
|
import warnings
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
from trio import (
|
from trio import (
|
||||||
|
@ -77,8 +78,8 @@ from ._exceptions import (
|
||||||
ContextCancelled,
|
ContextCancelled,
|
||||||
TransportClosed,
|
TransportClosed,
|
||||||
)
|
)
|
||||||
from ._discovery import get_arbiter
|
from .devx import _debug
|
||||||
from . import _debug
|
from ._discovery import get_registry
|
||||||
from ._portal import Portal
|
from ._portal import Portal
|
||||||
from . import _state
|
from . import _state
|
||||||
from . import _mp_fixup_main
|
from . import _mp_fixup_main
|
||||||
|
@ -127,6 +128,11 @@ class Actor:
|
||||||
# ugh, we need to get rid of this and replace with a "registry" sys
|
# ugh, we need to get rid of this and replace with a "registry" sys
|
||||||
# https://github.com/goodboy/tractor/issues/216
|
# https://github.com/goodboy/tractor/issues/216
|
||||||
is_arbiter: bool = False
|
is_arbiter: bool = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_registrar(self) -> bool:
|
||||||
|
return self.is_arbiter
|
||||||
|
|
||||||
msg_buffer_size: int = 2**6
|
msg_buffer_size: int = 2**6
|
||||||
|
|
||||||
# nursery placeholders filled in by `async_main()` after fork
|
# nursery placeholders filled in by `async_main()` after fork
|
||||||
|
@ -162,10 +168,14 @@ class Actor:
|
||||||
name: str,
|
name: str,
|
||||||
*,
|
*,
|
||||||
enable_modules: list[str] = [],
|
enable_modules: list[str] = [],
|
||||||
uid: str | None = None,
|
uid: str|None = None,
|
||||||
loglevel: str | None = None,
|
loglevel: str|None = None,
|
||||||
|
registry_addrs: list[tuple[str, int]]|None = None,
|
||||||
|
spawn_method: str|None = None,
|
||||||
|
|
||||||
|
# TODO: remove!
|
||||||
arbiter_addr: tuple[str, int] | None = None,
|
arbiter_addr: tuple[str, int] | None = None,
|
||||||
spawn_method: str | None = None
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
This constructor is called in the parent actor **before** the spawning
|
This constructor is called in the parent actor **before** the spawning
|
||||||
|
@ -187,29 +197,32 @@ class Actor:
|
||||||
self._parent_main_data = _mp_fixup_main._mp_figure_out_main()
|
self._parent_main_data = _mp_fixup_main._mp_figure_out_main()
|
||||||
|
|
||||||
# always include debugging tools module
|
# always include debugging tools module
|
||||||
enable_modules.append('tractor._debug')
|
enable_modules.append('tractor.devx._debug')
|
||||||
|
|
||||||
mods = {}
|
self.enable_modules: dict[str, str] = {}
|
||||||
for name in enable_modules:
|
for name in enable_modules:
|
||||||
mod = importlib.import_module(name)
|
mod: ModuleType = importlib.import_module(name)
|
||||||
mods[name] = _get_mod_abspath(mod)
|
self.enable_modules[name] = _get_mod_abspath(mod)
|
||||||
|
|
||||||
self.enable_modules = mods
|
|
||||||
self._mods: dict[str, ModuleType] = {}
|
self._mods: dict[str, ModuleType] = {}
|
||||||
self.loglevel = loglevel
|
self.loglevel: str = loglevel
|
||||||
|
|
||||||
self._arb_addr: tuple[str, int] | None = (
|
if arbiter_addr is not None:
|
||||||
str(arbiter_addr[0]),
|
warnings.warn(
|
||||||
int(arbiter_addr[1])
|
'`Actor(arbiter_addr=<blah>)` is now deprecated.\n'
|
||||||
) if arbiter_addr else None
|
'Use `registry_addrs: list[tuple]` instead.',
|
||||||
|
DeprecationWarning,
|
||||||
|
stacklevel=2,
|
||||||
|
)
|
||||||
|
registry_addrs: list[tuple[str, int]] = [arbiter_addr]
|
||||||
|
|
||||||
# marked by the process spawning backend at startup
|
# marked by the process spawning backend at startup
|
||||||
# will be None for the parent most process started manually
|
# will be None for the parent most process started manually
|
||||||
# by the user (currently called the "arbiter")
|
# by the user (currently called the "arbiter")
|
||||||
self._spawn_method = spawn_method
|
self._spawn_method: str = spawn_method
|
||||||
|
|
||||||
self._peers: defaultdict = defaultdict(list)
|
self._peers: defaultdict = defaultdict(list)
|
||||||
self._peer_connected: dict = {}
|
self._peer_connected: dict[tuple[str, str], trio.Event] = {}
|
||||||
self._no_more_peers = trio.Event()
|
self._no_more_peers = trio.Event()
|
||||||
self._no_more_peers.set()
|
self._no_more_peers.set()
|
||||||
self._ongoing_rpc_tasks = trio.Event()
|
self._ongoing_rpc_tasks = trio.Event()
|
||||||
|
@ -239,6 +252,45 @@ class Actor:
|
||||||
ActorNursery | None,
|
ActorNursery | None,
|
||||||
] = {} # type: ignore # noqa
|
] = {} # type: ignore # noqa
|
||||||
|
|
||||||
|
# when provided, init the registry addresses property from
|
||||||
|
# input via the validator.
|
||||||
|
self._reg_addrs: list[tuple[str, int]] = []
|
||||||
|
if registry_addrs:
|
||||||
|
self.reg_addrs: list[tuple[str, int]] = registry_addrs
|
||||||
|
_state._runtime_vars['_registry_addrs'] = registry_addrs
|
||||||
|
|
||||||
|
@property
|
||||||
|
def reg_addrs(self) -> list[tuple[str, int]]:
|
||||||
|
'''
|
||||||
|
List of (socket) addresses for all known (and contactable)
|
||||||
|
registry actors.
|
||||||
|
|
||||||
|
'''
|
||||||
|
return self._reg_addrs
|
||||||
|
|
||||||
|
@reg_addrs.setter
|
||||||
|
def reg_addrs(
|
||||||
|
self,
|
||||||
|
addrs: list[tuple[str, int]],
|
||||||
|
) -> None:
|
||||||
|
if not addrs:
|
||||||
|
log.warning(
|
||||||
|
'Empty registry address list is invalid:\n'
|
||||||
|
f'{addrs}'
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# always sanity check the input list since it's critical
|
||||||
|
# that addrs are correct for discovery sys operation.
|
||||||
|
for addr in addrs:
|
||||||
|
if not isinstance(addr, tuple):
|
||||||
|
raise ValueError(
|
||||||
|
'Expected `Actor.reg_addrs: list[tuple[str, int]]`\n'
|
||||||
|
f'Got {addrs}'
|
||||||
|
)
|
||||||
|
|
||||||
|
self._reg_addrs = addrs
|
||||||
|
|
||||||
async def wait_for_peer(
|
async def wait_for_peer(
|
||||||
self, uid: tuple[str, str]
|
self, uid: tuple[str, str]
|
||||||
) -> tuple[trio.Event, Channel]:
|
) -> tuple[trio.Event, Channel]:
|
||||||
|
@ -336,6 +388,12 @@ class Actor:
|
||||||
self._no_more_peers = trio.Event() # unset by making new
|
self._no_more_peers = trio.Event() # unset by making new
|
||||||
chan = Channel.from_stream(stream)
|
chan = Channel.from_stream(stream)
|
||||||
their_uid: tuple[str, str]|None = chan.uid
|
their_uid: tuple[str, str]|None = chan.uid
|
||||||
|
if their_uid:
|
||||||
|
log.warning(
|
||||||
|
f'Re-connection from already known {their_uid}'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
log.runtime(f'New connection to us @{chan.raddr}')
|
||||||
|
|
||||||
con_msg: str = ''
|
con_msg: str = ''
|
||||||
if their_uid:
|
if their_uid:
|
||||||
|
@ -517,16 +575,19 @@ class Actor:
|
||||||
|
|
||||||
if disconnected:
|
if disconnected:
|
||||||
# if the transport died and this actor is still
|
# if the transport died and this actor is still
|
||||||
# registered within a local nursery, we report that the
|
# registered within a local nursery, we report
|
||||||
# IPC layer may have failed unexpectedly since it may be
|
# that the IPC layer may have failed
|
||||||
# the cause of other downstream errors.
|
# unexpectedly since it may be the cause of
|
||||||
|
# other downstream errors.
|
||||||
entry = local_nursery._children.get(uid)
|
entry = local_nursery._children.get(uid)
|
||||||
if entry:
|
if entry:
|
||||||
proc: trio.Process
|
proc: trio.Process
|
||||||
_, proc, _ = entry
|
_, proc, _ = entry
|
||||||
|
|
||||||
poll = getattr(proc, 'poll', None)
|
if (
|
||||||
if poll and poll() is None:
|
(poll := getattr(proc, 'poll', None))
|
||||||
|
and poll() is None
|
||||||
|
):
|
||||||
log.cancel(
|
log.cancel(
|
||||||
f'Peer IPC broke but subproc is alive?\n\n'
|
f'Peer IPC broke but subproc is alive?\n\n'
|
||||||
|
|
||||||
|
@ -632,7 +693,7 @@ class Actor:
|
||||||
and not db_cs.cancel_called
|
and not db_cs.cancel_called
|
||||||
and uid == pdb_user_uid
|
and uid == pdb_user_uid
|
||||||
):
|
):
|
||||||
log.warning(
|
log.critical(
|
||||||
f'STALE DEBUG LOCK DETECTED FOR {uid}'
|
f'STALE DEBUG LOCK DETECTED FOR {uid}'
|
||||||
)
|
)
|
||||||
# TODO: figure out why this breaks tests..
|
# TODO: figure out why this breaks tests..
|
||||||
|
@ -880,11 +941,11 @@ class Actor:
|
||||||
)
|
)
|
||||||
await chan.connect()
|
await chan.connect()
|
||||||
|
|
||||||
|
# TODO: move this into a `Channel.handshake()`?
|
||||||
# Initial handshake: swap names.
|
# Initial handshake: swap names.
|
||||||
await self._do_handshake(chan)
|
await self._do_handshake(chan)
|
||||||
|
|
||||||
accept_addr: tuple[str, int] | None = None
|
accept_addrs: list[tuple[str, int]] | None = None
|
||||||
|
|
||||||
if self._spawn_method == "trio":
|
if self._spawn_method == "trio":
|
||||||
# Receive runtime state from our parent
|
# Receive runtime state from our parent
|
||||||
parent_data: dict[str, Any]
|
parent_data: dict[str, Any]
|
||||||
|
@ -897,10 +958,7 @@ class Actor:
|
||||||
# if "trace"/"util" mode is enabled?
|
# if "trace"/"util" mode is enabled?
|
||||||
f'{pformat(parent_data)}\n'
|
f'{pformat(parent_data)}\n'
|
||||||
)
|
)
|
||||||
accept_addr = (
|
accept_addrs: list[tuple[str, int]] = parent_data.pop('bind_addrs')
|
||||||
parent_data.pop('bind_host'),
|
|
||||||
parent_data.pop('bind_port'),
|
|
||||||
)
|
|
||||||
rvs = parent_data.pop('_runtime_vars')
|
rvs = parent_data.pop('_runtime_vars')
|
||||||
|
|
||||||
if rvs['_debug_mode']:
|
if rvs['_debug_mode']:
|
||||||
|
@ -918,18 +976,23 @@ class Actor:
|
||||||
_state._runtime_vars.update(rvs)
|
_state._runtime_vars.update(rvs)
|
||||||
|
|
||||||
for attr, value in parent_data.items():
|
for attr, value in parent_data.items():
|
||||||
|
if (
|
||||||
if attr == '_arb_addr':
|
attr == 'reg_addrs'
|
||||||
|
and value
|
||||||
|
):
|
||||||
# XXX: ``msgspec`` doesn't support serializing tuples
|
# XXX: ``msgspec`` doesn't support serializing tuples
|
||||||
# so just cash manually here since it's what our
|
# so just cash manually here since it's what our
|
||||||
# internals expect.
|
# internals expect.
|
||||||
value = tuple(value) if value else None
|
# TODO: we don't really NEED these as
|
||||||
self._arb_addr = value
|
# tuples so we can probably drop this
|
||||||
|
# casting since apparently in python lists
|
||||||
|
# are "more efficient"?
|
||||||
|
self.reg_addrs = [tuple(val) for val in value]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
setattr(self, attr, value)
|
setattr(self, attr, value)
|
||||||
|
|
||||||
return chan, accept_addr
|
return chan, accept_addrs
|
||||||
|
|
||||||
except OSError: # failed to connect
|
except OSError: # failed to connect
|
||||||
log.warning(
|
log.warning(
|
||||||
|
@ -946,9 +1009,9 @@ class Actor:
|
||||||
handler_nursery: Nursery,
|
handler_nursery: Nursery,
|
||||||
*,
|
*,
|
||||||
# (host, port) to bind for channel server
|
# (host, port) to bind for channel server
|
||||||
accept_host: tuple[str, int] | None = None,
|
listen_sockaddrs: list[tuple[str, int]] | None = None,
|
||||||
accept_port: int = 0,
|
|
||||||
task_status: TaskStatus[trio.Nursery] = trio.TASK_STATUS_IGNORED,
|
task_status: TaskStatus[Nursery] = trio.TASK_STATUS_IGNORED,
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Start the IPC transport server, begin listening for new connections.
|
Start the IPC transport server, begin listening for new connections.
|
||||||
|
@ -958,30 +1021,40 @@ class Actor:
|
||||||
`.cancel_server()` is called.
|
`.cancel_server()` is called.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
if listen_sockaddrs is None:
|
||||||
|
listen_sockaddrs = [(None, 0)]
|
||||||
|
|
||||||
self._server_down = trio.Event()
|
self._server_down = trio.Event()
|
||||||
try:
|
try:
|
||||||
async with trio.open_nursery() as server_n:
|
async with trio.open_nursery() as server_n:
|
||||||
listeners: list[trio.abc.Listener] = await server_n.start(
|
|
||||||
partial(
|
for host, port in listen_sockaddrs:
|
||||||
trio.serve_tcp,
|
listeners: list[trio.abc.Listener] = await server_n.start(
|
||||||
self._stream_handler,
|
partial(
|
||||||
# new connections will stay alive even if this server
|
trio.serve_tcp,
|
||||||
# is cancelled
|
|
||||||
handler_nursery=handler_nursery,
|
handler=self._stream_handler,
|
||||||
port=accept_port,
|
port=port,
|
||||||
host=accept_host,
|
host=host,
|
||||||
|
|
||||||
|
# NOTE: configured such that new
|
||||||
|
# connections will stay alive even if
|
||||||
|
# this server is cancelled!
|
||||||
|
handler_nursery=handler_nursery,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
sockets: list[trio.socket] = [
|
||||||
sockets: list[trio.socket] = [
|
getattr(listener, 'socket', 'unknown socket')
|
||||||
getattr(listener, 'socket', 'unknown socket')
|
for listener in listeners
|
||||||
for listener in listeners
|
]
|
||||||
]
|
log.runtime(
|
||||||
log.runtime(
|
'Started TCP server(s)\n'
|
||||||
'Started TCP server(s)\n'
|
f'|_{sockets}\n'
|
||||||
f'|_{sockets}\n'
|
)
|
||||||
)
|
self._listeners.extend(listeners)
|
||||||
self._listeners.extend(listeners)
|
|
||||||
task_status.started(server_n)
|
task_status.started(server_n)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# signal the server is down since nursery above terminated
|
# signal the server is down since nursery above terminated
|
||||||
self._server_down.set()
|
self._server_down.set()
|
||||||
|
@ -1319,6 +1392,18 @@ class Actor:
|
||||||
self._server_n.cancel_scope.cancel()
|
self._server_n.cancel_scope.cancel()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
def accept_addrs(self) -> list[tuple[str, int]]:
|
||||||
|
'''
|
||||||
|
All addresses to which the transport-channel server binds
|
||||||
|
and listens for new connections.
|
||||||
|
|
||||||
|
'''
|
||||||
|
# throws OSError on failure
|
||||||
|
return [
|
||||||
|
listener.socket.getsockname()
|
||||||
|
for listener in self._listeners
|
||||||
|
] # type: ignore
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def accept_addr(self) -> tuple[str, int]:
|
def accept_addr(self) -> tuple[str, int]:
|
||||||
'''
|
'''
|
||||||
|
@ -1327,7 +1412,7 @@ class Actor:
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# throws OSError on failure
|
# throws OSError on failure
|
||||||
return self._listeners[0].socket.getsockname() # type: ignore
|
return self.accept_addrs[0]
|
||||||
|
|
||||||
def get_parent(self) -> Portal:
|
def get_parent(self) -> Portal:
|
||||||
'''
|
'''
|
||||||
|
@ -1344,6 +1429,7 @@ class Actor:
|
||||||
'''
|
'''
|
||||||
return self._peers[uid]
|
return self._peers[uid]
|
||||||
|
|
||||||
|
# TODO: move to `Channel.handshake(uid)`
|
||||||
async def _do_handshake(
|
async def _do_handshake(
|
||||||
self,
|
self,
|
||||||
chan: Channel
|
chan: Channel
|
||||||
|
@ -1380,7 +1466,7 @@ class Actor:
|
||||||
|
|
||||||
async def async_main(
|
async def async_main(
|
||||||
actor: Actor,
|
actor: Actor,
|
||||||
accept_addr: tuple[str, int] | None = None,
|
accept_addrs: tuple[str, int] | None = None,
|
||||||
|
|
||||||
# XXX: currently ``parent_addr`` is only needed for the
|
# XXX: currently ``parent_addr`` is only needed for the
|
||||||
# ``multiprocessing`` backend (which pickles state sent to
|
# ``multiprocessing`` backend (which pickles state sent to
|
||||||
|
@ -1408,20 +1494,25 @@ async def async_main(
|
||||||
# on our debugger lock state.
|
# on our debugger lock state.
|
||||||
_debug.Lock._trio_handler = signal.getsignal(signal.SIGINT)
|
_debug.Lock._trio_handler = signal.getsignal(signal.SIGINT)
|
||||||
|
|
||||||
registered_with_arbiter = False
|
is_registered: bool = False
|
||||||
try:
|
try:
|
||||||
|
|
||||||
# establish primary connection with immediate parent
|
# establish primary connection with immediate parent
|
||||||
actor._parent_chan = None
|
actor._parent_chan: Channel | None = None
|
||||||
if parent_addr is not None:
|
if parent_addr is not None:
|
||||||
|
|
||||||
actor._parent_chan, accept_addr_rent = await actor._from_parent(
|
(
|
||||||
parent_addr)
|
actor._parent_chan,
|
||||||
|
set_accept_addr_says_rent,
|
||||||
|
) = await actor._from_parent(parent_addr)
|
||||||
|
|
||||||
# either it's passed in because we're not a child
|
# either it's passed in because we're not a child or
|
||||||
# or because we're running in mp mode
|
# because we're running in mp mode
|
||||||
if accept_addr_rent is not None:
|
if (
|
||||||
accept_addr = accept_addr_rent
|
set_accept_addr_says_rent
|
||||||
|
and set_accept_addr_says_rent is not None
|
||||||
|
):
|
||||||
|
accept_addrs = set_accept_addr_says_rent
|
||||||
|
|
||||||
# The "root" nursery ensures the channel with the immediate
|
# The "root" nursery ensures the channel with the immediate
|
||||||
# parent is kept alive as a resilient service until
|
# parent is kept alive as a resilient service until
|
||||||
|
@ -1461,34 +1552,72 @@ async def async_main(
|
||||||
# - subactor: the bind address is sent by our parent
|
# - subactor: the bind address is sent by our parent
|
||||||
# over our established channel
|
# over our established channel
|
||||||
# - root actor: the ``accept_addr`` passed to this method
|
# - root actor: the ``accept_addr`` passed to this method
|
||||||
assert accept_addr
|
assert accept_addrs
|
||||||
host, port = accept_addr
|
|
||||||
|
|
||||||
actor._server_n = await service_nursery.start(
|
try:
|
||||||
partial(
|
actor._server_n = await service_nursery.start(
|
||||||
actor._serve_forever,
|
partial(
|
||||||
service_nursery,
|
actor._serve_forever,
|
||||||
accept_host=host,
|
service_nursery,
|
||||||
accept_port=port
|
listen_sockaddrs=accept_addrs,
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
except OSError as oserr:
|
||||||
accept_addr = actor.accept_addr
|
# NOTE: always allow runtime hackers to debug
|
||||||
|
# tranport address bind errors - normally it's
|
||||||
|
# something silly like the wrong socket-address
|
||||||
|
# passed via a config or CLI Bo
|
||||||
|
entered_debug: bool = await _debug._maybe_enter_pm(oserr)
|
||||||
|
if not entered_debug:
|
||||||
|
log.exception('Failed to init IPC channel server !?\n')
|
||||||
|
raise
|
||||||
|
|
||||||
|
accept_addrs: list[tuple[str, int]] = actor.accept_addrs
|
||||||
|
|
||||||
|
# NOTE: only set the loopback addr for the
|
||||||
|
# process-tree-global "root" mailbox since
|
||||||
|
# all sub-actors should be able to speak to
|
||||||
|
# their root actor over that channel.
|
||||||
if _state._runtime_vars['_is_root']:
|
if _state._runtime_vars['_is_root']:
|
||||||
_state._runtime_vars['_root_mailbox'] = accept_addr
|
for addr in accept_addrs:
|
||||||
|
host, _ = addr
|
||||||
|
# TODO: generic 'lo' detector predicate
|
||||||
|
if '127.0.0.1' in host:
|
||||||
|
_state._runtime_vars['_root_mailbox'] = addr
|
||||||
|
|
||||||
# Register with the arbiter if we're told its addr
|
# Register with the arbiter if we're told its addr
|
||||||
log.runtime(f"Registering {actor} for role `{actor.name}`")
|
log.runtime(
|
||||||
assert isinstance(actor._arb_addr, tuple)
|
f'Registering `{actor.name}` ->\n'
|
||||||
|
f'{pformat(accept_addrs)}'
|
||||||
|
)
|
||||||
|
|
||||||
async with get_arbiter(*actor._arb_addr) as arb_portal:
|
# TODO: ideally we don't fan out to all registrars
|
||||||
await arb_portal.run_from_ns(
|
# if addresses point to the same actor..
|
||||||
'self',
|
# So we need a way to detect that? maybe iterate
|
||||||
'register_actor',
|
# only on unique actor uids?
|
||||||
uid=actor.uid,
|
for addr in actor.reg_addrs:
|
||||||
sockaddr=accept_addr,
|
try:
|
||||||
)
|
assert isinstance(addr, tuple)
|
||||||
|
assert addr[1] # non-zero after bind
|
||||||
|
except AssertionError:
|
||||||
|
await _debug.pause()
|
||||||
|
|
||||||
registered_with_arbiter = True
|
async with get_registry(*addr) as reg_portal:
|
||||||
|
for accept_addr in accept_addrs:
|
||||||
|
|
||||||
|
if not accept_addr[1]:
|
||||||
|
await _debug.pause()
|
||||||
|
|
||||||
|
assert accept_addr[1]
|
||||||
|
|
||||||
|
await reg_portal.run_from_ns(
|
||||||
|
'self',
|
||||||
|
'register_actor',
|
||||||
|
uid=actor.uid,
|
||||||
|
sockaddr=accept_addr,
|
||||||
|
)
|
||||||
|
|
||||||
|
is_registered: bool = True
|
||||||
|
|
||||||
# init steps complete
|
# init steps complete
|
||||||
task_status.started()
|
task_status.started()
|
||||||
|
@ -1521,18 +1650,20 @@ async def async_main(
|
||||||
log.runtime("Closing all actor lifetime contexts")
|
log.runtime("Closing all actor lifetime contexts")
|
||||||
actor.lifetime_stack.close()
|
actor.lifetime_stack.close()
|
||||||
|
|
||||||
if not registered_with_arbiter:
|
if not is_registered:
|
||||||
# TODO: I guess we could try to connect back
|
# TODO: I guess we could try to connect back
|
||||||
# to the parent through a channel and engage a debugger
|
# to the parent through a channel and engage a debugger
|
||||||
# once we have that all working with std streams locking?
|
# once we have that all working with std streams locking?
|
||||||
log.exception(
|
log.exception(
|
||||||
f"Actor errored and failed to register with arbiter "
|
f"Actor errored and failed to register with arbiter "
|
||||||
f"@ {actor._arb_addr}?")
|
f"@ {actor.reg_addrs[0]}?")
|
||||||
log.error(
|
log.error(
|
||||||
"\n\n\t^^^ THIS IS PROBABLY A TRACTOR BUGGGGG!!! ^^^\n"
|
"\n\n\t^^^ THIS IS PROBABLY AN INTERNAL `tractor` BUG! ^^^\n\n"
|
||||||
"\tCALMLY CALL THE AUTHORITIES AND HIDE YOUR CHILDREN.\n\n"
|
"\t>> CALMLY CALL THE AUTHORITIES AND HIDE YOUR CHILDREN <<\n\n"
|
||||||
"\tYOUR PARENT CODE IS GOING TO KEEP WORKING FINE!!!\n"
|
"\tIf this is a sub-actor hopefully its parent will keep running "
|
||||||
"\tTHIS IS HOW RELIABlE SYSTEMS ARE SUPPOSED TO WORK!?!?\n"
|
"correctly presuming this error was safely ignored..\n\n"
|
||||||
|
"\tPLEASE REPORT THIS TRACEBACK IN A BUG REPORT: "
|
||||||
|
"https://github.com/goodboy/tractor/issues\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
if actor._parent_chan:
|
if actor._parent_chan:
|
||||||
|
@ -1572,27 +1703,33 @@ async def async_main(
|
||||||
|
|
||||||
# Unregister actor from the registry-sys / registrar.
|
# Unregister actor from the registry-sys / registrar.
|
||||||
if (
|
if (
|
||||||
registered_with_arbiter
|
is_registered
|
||||||
and not actor.is_arbiter
|
and not actor.is_registrar
|
||||||
):
|
):
|
||||||
failed = False
|
failed: bool = False
|
||||||
assert isinstance(actor._arb_addr, tuple)
|
for addr in actor.reg_addrs:
|
||||||
with trio.move_on_after(0.5) as cs:
|
assert isinstance(addr, tuple)
|
||||||
cs.shield = True
|
with trio.move_on_after(0.5) as cs:
|
||||||
try:
|
cs.shield = True
|
||||||
async with get_arbiter(*actor._arb_addr) as arb_portal:
|
try:
|
||||||
await arb_portal.run_from_ns(
|
async with get_registry(
|
||||||
'self',
|
*addr,
|
||||||
'unregister_actor',
|
) as reg_portal:
|
||||||
uid=actor.uid
|
await reg_portal.run_from_ns(
|
||||||
)
|
'self',
|
||||||
except OSError:
|
'unregister_actor',
|
||||||
|
uid=actor.uid
|
||||||
|
)
|
||||||
|
except OSError:
|
||||||
|
failed = True
|
||||||
|
if cs.cancelled_caught:
|
||||||
failed = True
|
failed = True
|
||||||
if cs.cancelled_caught:
|
|
||||||
failed = True
|
if failed:
|
||||||
if failed:
|
log.warning(
|
||||||
log.warning(
|
f'Failed to unregister {actor.name} from '
|
||||||
f"Failed to unregister {actor.name} from arbiter")
|
f'registar @ {addr}'
|
||||||
|
)
|
||||||
|
|
||||||
# Ensure all peers (actors connected to us as clients) are finished
|
# Ensure all peers (actors connected to us as clients) are finished
|
||||||
if not actor._no_more_peers.is_set():
|
if not actor._no_more_peers.is_set():
|
||||||
|
@ -1611,18 +1748,36 @@ async def async_main(
|
||||||
# TODO: rename to `Registry` and move to `._discovery`!
|
# TODO: rename to `Registry` and move to `._discovery`!
|
||||||
class Arbiter(Actor):
|
class Arbiter(Actor):
|
||||||
'''
|
'''
|
||||||
A special actor who knows all the other actors and always has
|
A special registrar actor who can contact all other actors
|
||||||
access to a top level nursery.
|
within its immediate process tree and possibly keeps a registry
|
||||||
|
of others meant to be discoverable in a distributed
|
||||||
|
application. Normally the registrar is also the "root actor"
|
||||||
|
and thus always has access to the top-most-level actor
|
||||||
|
(process) nursery.
|
||||||
|
|
||||||
The arbiter is by default the first actor spawned on each host
|
By default, the registrar is always initialized when and if no
|
||||||
and is responsible for keeping track of all other actors for
|
other registrar socket addrs have been specified to runtime
|
||||||
coordination purposes. If a new main process is launched and an
|
init entry-points (such as `open_root_actor()` or
|
||||||
arbiter is already running that arbiter will be used.
|
`open_nursery()`). Any time a new main process is launched (and
|
||||||
|
thus thus a new root actor created) and, no existing registrar
|
||||||
|
can be contacted at the provided `registry_addr`, then a new
|
||||||
|
one is always created; however, if one can be reached it is
|
||||||
|
used.
|
||||||
|
|
||||||
|
Normally a distributed app requires at least registrar per
|
||||||
|
logical host where for that given "host space" (aka localhost
|
||||||
|
IPC domain of addresses) it is responsible for making all other
|
||||||
|
host (local address) bound actors *discoverable* to external
|
||||||
|
actor trees running on remote hosts.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
is_arbiter = True
|
is_arbiter = True
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs) -> None:
|
def __init__(
|
||||||
|
self,
|
||||||
|
*args,
|
||||||
|
**kwargs,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
self._registry: dict[
|
self._registry: dict[
|
||||||
tuple[str, str],
|
tuple[str, str],
|
||||||
|
@ -1664,7 +1819,10 @@ class Arbiter(Actor):
|
||||||
# unpacker since we have tuples as keys (not this makes the
|
# unpacker since we have tuples as keys (not this makes the
|
||||||
# arbiter suscetible to hashdos):
|
# arbiter suscetible to hashdos):
|
||||||
# https://github.com/msgpack/msgpack-python#major-breaking-changes-in-msgpack-10
|
# https://github.com/msgpack/msgpack-python#major-breaking-changes-in-msgpack-10
|
||||||
return {'.'.join(key): val for key, val in self._registry.items()}
|
return {
|
||||||
|
'.'.join(key): val
|
||||||
|
for key, val in self._registry.items()
|
||||||
|
}
|
||||||
|
|
||||||
async def wait_for_actor(
|
async def wait_for_actor(
|
||||||
self,
|
self,
|
||||||
|
@ -1707,8 +1865,15 @@ class Arbiter(Actor):
|
||||||
sockaddr: tuple[str, int]
|
sockaddr: tuple[str, int]
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
uid = name, _ = (str(uid[0]), str(uid[1]))
|
uid = name, hash = (str(uid[0]), str(uid[1]))
|
||||||
self._registry[uid] = (str(sockaddr[0]), int(sockaddr[1]))
|
addr = (host, port) = (
|
||||||
|
str(sockaddr[0]),
|
||||||
|
int(sockaddr[1]),
|
||||||
|
)
|
||||||
|
if port == 0:
|
||||||
|
await _debug.pause()
|
||||||
|
assert port # should never be 0-dynamic-os-alloc
|
||||||
|
self._registry[uid] = addr
|
||||||
|
|
||||||
# pop and signal all waiter events
|
# pop and signal all waiter events
|
||||||
events = self._waiters.pop(name, [])
|
events = self._waiters.pop(name, [])
|
||||||
|
@ -1723,4 +1888,6 @@ class Arbiter(Actor):
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
uid = (str(uid[0]), str(uid[1]))
|
uid = (str(uid[0]), str(uid[1]))
|
||||||
self._registry.pop(uid)
|
entry: tuple = self._registry.pop(uid, None)
|
||||||
|
if entry is None:
|
||||||
|
log.warning(f'Request to de-register {uid} failed?')
|
||||||
|
|
|
@ -34,7 +34,7 @@ from typing import (
|
||||||
import trio
|
import trio
|
||||||
from trio import TaskStatus
|
from trio import TaskStatus
|
||||||
|
|
||||||
from ._debug import (
|
from .devx._debug import (
|
||||||
maybe_wait_for_debugger,
|
maybe_wait_for_debugger,
|
||||||
acquire_debug_lock,
|
acquire_debug_lock,
|
||||||
)
|
)
|
||||||
|
@ -220,6 +220,10 @@ async def hard_kill(
|
||||||
# whilst also hacking on it XD
|
# whilst also hacking on it XD
|
||||||
# terminate_after: int = 99999,
|
# terminate_after: int = 99999,
|
||||||
|
|
||||||
|
# NOTE: for mucking with `.pause()`-ing inside the runtime
|
||||||
|
# whilst also hacking on it XD
|
||||||
|
# terminate_after: int = 99999,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
'''
|
'''
|
||||||
Un-gracefully terminate an OS level `trio.Process` after timeout.
|
Un-gracefully terminate an OS level `trio.Process` after timeout.
|
||||||
|
@ -365,7 +369,7 @@ async def new_proc(
|
||||||
errors: dict[tuple[str, str], Exception],
|
errors: dict[tuple[str, str], Exception],
|
||||||
|
|
||||||
# passed through to actor main
|
# passed through to actor main
|
||||||
bind_addr: tuple[str, int],
|
bind_addrs: list[tuple[str, int]],
|
||||||
parent_addr: tuple[str, int],
|
parent_addr: tuple[str, int],
|
||||||
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
||||||
|
|
||||||
|
@ -387,7 +391,7 @@ async def new_proc(
|
||||||
actor_nursery,
|
actor_nursery,
|
||||||
subactor,
|
subactor,
|
||||||
errors,
|
errors,
|
||||||
bind_addr,
|
bind_addrs,
|
||||||
parent_addr,
|
parent_addr,
|
||||||
_runtime_vars, # run time vars
|
_runtime_vars, # run time vars
|
||||||
infect_asyncio=infect_asyncio,
|
infect_asyncio=infect_asyncio,
|
||||||
|
@ -402,7 +406,7 @@ async def trio_proc(
|
||||||
errors: dict[tuple[str, str], Exception],
|
errors: dict[tuple[str, str], Exception],
|
||||||
|
|
||||||
# passed through to actor main
|
# passed through to actor main
|
||||||
bind_addr: tuple[str, int],
|
bind_addrs: list[tuple[str, int]],
|
||||||
parent_addr: tuple[str, int],
|
parent_addr: tuple[str, int],
|
||||||
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
||||||
*,
|
*,
|
||||||
|
@ -491,12 +495,11 @@ async def trio_proc(
|
||||||
|
|
||||||
# send additional init params
|
# send additional init params
|
||||||
await chan.send({
|
await chan.send({
|
||||||
"_parent_main_data": subactor._parent_main_data,
|
'_parent_main_data': subactor._parent_main_data,
|
||||||
"enable_modules": subactor.enable_modules,
|
'enable_modules': subactor.enable_modules,
|
||||||
"_arb_addr": subactor._arb_addr,
|
'reg_addrs': subactor.reg_addrs,
|
||||||
"bind_host": bind_addr[0],
|
'bind_addrs': bind_addrs,
|
||||||
"bind_port": bind_addr[1],
|
'_runtime_vars': _runtime_vars,
|
||||||
"_runtime_vars": _runtime_vars,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
# track subactor in current nursery
|
# track subactor in current nursery
|
||||||
|
@ -551,13 +554,14 @@ async def trio_proc(
|
||||||
with trio.move_on_after(0.5):
|
with trio.move_on_after(0.5):
|
||||||
await proc.wait()
|
await proc.wait()
|
||||||
|
|
||||||
log.pdb(
|
|
||||||
'Delaying subproc reaper while debugger locked..'
|
|
||||||
)
|
|
||||||
await maybe_wait_for_debugger(
|
await maybe_wait_for_debugger(
|
||||||
child_in_debug=_runtime_vars.get(
|
child_in_debug=_runtime_vars.get(
|
||||||
'_debug_mode', False
|
'_debug_mode', False
|
||||||
),
|
),
|
||||||
|
header_msg=(
|
||||||
|
'Delaying subproc reaper while debugger locked..\n'
|
||||||
|
),
|
||||||
|
|
||||||
# TODO: need a diff value then default?
|
# TODO: need a diff value then default?
|
||||||
# poll_steps=9999999,
|
# poll_steps=9999999,
|
||||||
)
|
)
|
||||||
|
@ -601,7 +605,7 @@ async def mp_proc(
|
||||||
subactor: Actor,
|
subactor: Actor,
|
||||||
errors: dict[tuple[str, str], Exception],
|
errors: dict[tuple[str, str], Exception],
|
||||||
# passed through to actor main
|
# passed through to actor main
|
||||||
bind_addr: tuple[str, int],
|
bind_addrs: list[tuple[str, int]],
|
||||||
parent_addr: tuple[str, int],
|
parent_addr: tuple[str, int],
|
||||||
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
_runtime_vars: dict[str, Any], # serialized and sent to _child
|
||||||
*,
|
*,
|
||||||
|
@ -659,7 +663,7 @@ async def mp_proc(
|
||||||
target=_mp_main,
|
target=_mp_main,
|
||||||
args=(
|
args=(
|
||||||
subactor,
|
subactor,
|
||||||
bind_addr,
|
bind_addrs,
|
||||||
fs_info,
|
fs_info,
|
||||||
_spawn_method,
|
_spawn_method,
|
||||||
parent_addr,
|
parent_addr,
|
||||||
|
|
|
@ -33,7 +33,8 @@ _last_actor_terminated: Actor|None = None
|
||||||
_runtime_vars: dict[str, Any] = {
|
_runtime_vars: dict[str, Any] = {
|
||||||
'_debug_mode': False,
|
'_debug_mode': False,
|
||||||
'_is_root': False,
|
'_is_root': False,
|
||||||
'_root_mailbox': (None, None)
|
'_root_mailbox': (None, None),
|
||||||
|
'_registry_addrs': [],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -22,16 +22,13 @@ from contextlib import asynccontextmanager as acm
|
||||||
from functools import partial
|
from functools import partial
|
||||||
import inspect
|
import inspect
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
from typing import (
|
from typing import TYPE_CHECKING
|
||||||
Optional,
|
|
||||||
TYPE_CHECKING,
|
|
||||||
)
|
|
||||||
import typing
|
import typing
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
|
|
||||||
from ._debug import maybe_wait_for_debugger
|
from .devx._debug import maybe_wait_for_debugger
|
||||||
from ._state import current_actor, is_main_process
|
from ._state import current_actor, is_main_process
|
||||||
from .log import get_logger, get_loglevel
|
from .log import get_logger, get_loglevel
|
||||||
from ._runtime import Actor
|
from ._runtime import Actor
|
||||||
|
@ -97,7 +94,7 @@ class ActorNursery:
|
||||||
tuple[
|
tuple[
|
||||||
Actor,
|
Actor,
|
||||||
trio.Process | mp.Process,
|
trio.Process | mp.Process,
|
||||||
Optional[Portal],
|
Portal | None,
|
||||||
]
|
]
|
||||||
] = {}
|
] = {}
|
||||||
# portals spawned with ``run_in_actor()`` are
|
# portals spawned with ``run_in_actor()`` are
|
||||||
|
@ -121,12 +118,12 @@ class ActorNursery:
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
*,
|
*,
|
||||||
bind_addr: tuple[str, int] = _default_bind_addr,
|
bind_addrs: list[tuple[str, int]] = [_default_bind_addr],
|
||||||
rpc_module_paths: list[str] | None = None,
|
rpc_module_paths: list[str] | None = None,
|
||||||
enable_modules: list[str] | None = None,
|
enable_modules: list[str] | None = None,
|
||||||
loglevel: str | None = None, # set log level per subactor
|
loglevel: str | None = None, # set log level per subactor
|
||||||
nursery: trio.Nursery | None = None,
|
nursery: trio.Nursery | None = None,
|
||||||
debug_mode: Optional[bool] | None = None,
|
debug_mode: bool | None = None,
|
||||||
infect_asyncio: bool = False,
|
infect_asyncio: bool = False,
|
||||||
) -> Portal:
|
) -> Portal:
|
||||||
'''
|
'''
|
||||||
|
@ -161,7 +158,9 @@ class ActorNursery:
|
||||||
# modules allowed to invoked funcs from
|
# modules allowed to invoked funcs from
|
||||||
enable_modules=enable_modules,
|
enable_modules=enable_modules,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
arbiter_addr=current_actor()._arb_addr,
|
|
||||||
|
# verbatim relay this actor's registrar addresses
|
||||||
|
registry_addrs=current_actor().reg_addrs,
|
||||||
)
|
)
|
||||||
parent_addr = self._actor.accept_addr
|
parent_addr = self._actor.accept_addr
|
||||||
assert parent_addr
|
assert parent_addr
|
||||||
|
@ -178,7 +177,7 @@ class ActorNursery:
|
||||||
self,
|
self,
|
||||||
subactor,
|
subactor,
|
||||||
self.errors,
|
self.errors,
|
||||||
bind_addr,
|
bind_addrs,
|
||||||
parent_addr,
|
parent_addr,
|
||||||
_rtv, # run time vars
|
_rtv, # run time vars
|
||||||
infect_asyncio=infect_asyncio,
|
infect_asyncio=infect_asyncio,
|
||||||
|
@ -191,8 +190,8 @@ class ActorNursery:
|
||||||
fn: typing.Callable,
|
fn: typing.Callable,
|
||||||
*,
|
*,
|
||||||
|
|
||||||
name: Optional[str] = None,
|
name: str | None = None,
|
||||||
bind_addr: tuple[str, int] = _default_bind_addr,
|
bind_addrs: tuple[str, int] = [_default_bind_addr],
|
||||||
rpc_module_paths: list[str] | None = None,
|
rpc_module_paths: list[str] | None = None,
|
||||||
enable_modules: list[str] | None = None,
|
enable_modules: list[str] | None = None,
|
||||||
loglevel: str | None = None, # set log level per subactor
|
loglevel: str | None = None, # set log level per subactor
|
||||||
|
@ -221,7 +220,7 @@ class ActorNursery:
|
||||||
enable_modules=[mod_path] + (
|
enable_modules=[mod_path] + (
|
||||||
enable_modules or rpc_module_paths or []
|
enable_modules or rpc_module_paths or []
|
||||||
),
|
),
|
||||||
bind_addr=bind_addr,
|
bind_addrs=bind_addrs,
|
||||||
loglevel=loglevel,
|
loglevel=loglevel,
|
||||||
# use the run_in_actor nursery
|
# use the run_in_actor nursery
|
||||||
nursery=self._ria_nursery,
|
nursery=self._ria_nursery,
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
# 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/>.
|
||||||
|
|
||||||
|
"""
|
||||||
|
Runtime "developer experience" utils and addons to aid our
|
||||||
|
(advanced) users and core devs in building distributed applications
|
||||||
|
and working with/on the actor runtime.
|
||||||
|
|
||||||
|
"""
|
||||||
|
from ._debug import (
|
||||||
|
maybe_wait_for_debugger as maybe_wait_for_debugger,
|
||||||
|
acquire_debug_lock as acquire_debug_lock,
|
||||||
|
breakpoint as breakpoint,
|
||||||
|
pause as pause,
|
||||||
|
pause_from_sync as pause_from_sync,
|
||||||
|
shield_sigint_handler as shield_sigint_handler,
|
||||||
|
MultiActorPdb as MultiActorPdb,
|
||||||
|
open_crash_handler as open_crash_handler,
|
||||||
|
maybe_open_crash_handler as maybe_open_crash_handler,
|
||||||
|
post_mortem as post_mortem,
|
||||||
|
)
|
||||||
|
from ._stackscope import (
|
||||||
|
enable_stack_on_sig as enable_stack_on_sig,
|
||||||
|
)
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,84 @@
|
||||||
|
# tractor: structured concurrent "actors".
|
||||||
|
# Copyright 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/>.
|
||||||
|
|
||||||
|
'''
|
||||||
|
The fundamental cross process SC abstraction: an inter-actor,
|
||||||
|
cancel-scope linked task "context".
|
||||||
|
|
||||||
|
A ``Context`` is very similar to the ``trio.Nursery.cancel_scope`` built
|
||||||
|
into each ``trio.Nursery`` except it links the lifetimes of memory space
|
||||||
|
disjoint, parallel executing tasks in separate actors.
|
||||||
|
|
||||||
|
'''
|
||||||
|
from signal import (
|
||||||
|
signal,
|
||||||
|
SIGUSR1,
|
||||||
|
)
|
||||||
|
|
||||||
|
import trio
|
||||||
|
|
||||||
|
@trio.lowlevel.disable_ki_protection
|
||||||
|
def dump_task_tree() -> None:
|
||||||
|
import stackscope
|
||||||
|
from tractor.log import get_console_log
|
||||||
|
|
||||||
|
tree_str: str = str(
|
||||||
|
stackscope.extract(
|
||||||
|
trio.lowlevel.current_root_task(),
|
||||||
|
recurse_child_tasks=True
|
||||||
|
)
|
||||||
|
)
|
||||||
|
log = get_console_log('cancel')
|
||||||
|
log.pdb(
|
||||||
|
f'Dumping `stackscope` tree:\n\n'
|
||||||
|
f'{tree_str}\n'
|
||||||
|
)
|
||||||
|
# import logging
|
||||||
|
# try:
|
||||||
|
# with open("/dev/tty", "w") as tty:
|
||||||
|
# tty.write(tree_str)
|
||||||
|
# except BaseException:
|
||||||
|
# logging.getLogger(
|
||||||
|
# "task_tree"
|
||||||
|
# ).exception("Error printing task tree")
|
||||||
|
|
||||||
|
|
||||||
|
def signal_handler(sig: int, frame: object) -> None:
|
||||||
|
import traceback
|
||||||
|
try:
|
||||||
|
trio.lowlevel.current_trio_token(
|
||||||
|
).run_sync_soon(dump_task_tree)
|
||||||
|
except RuntimeError:
|
||||||
|
# not in async context -- print a normal traceback
|
||||||
|
traceback.print_stack()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def enable_stack_on_sig(
|
||||||
|
sig: int = SIGUSR1
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Enable `stackscope` tracing on reception of a signal; by
|
||||||
|
default this is SIGUSR1.
|
||||||
|
|
||||||
|
'''
|
||||||
|
signal(
|
||||||
|
sig,
|
||||||
|
signal_handler,
|
||||||
|
)
|
||||||
|
# NOTE: not the above can be triggered from
|
||||||
|
# a (xonsh) shell using:
|
||||||
|
# kill -SIGUSR1 @$(pgrep -f '<cmd>')
|
|
@ -0,0 +1,129 @@
|
||||||
|
# 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/>.
|
||||||
|
|
||||||
|
"""
|
||||||
|
CLI framework extensions for hacking on the actor runtime.
|
||||||
|
|
||||||
|
Currently popular frameworks supported are:
|
||||||
|
|
||||||
|
- `typer` via the `@callback` API
|
||||||
|
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
from typing import (
|
||||||
|
Any,
|
||||||
|
Callable,
|
||||||
|
)
|
||||||
|
from typing_extensions import Annotated
|
||||||
|
|
||||||
|
import typer
|
||||||
|
|
||||||
|
|
||||||
|
_runtime_vars: dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
||||||
|
def load_runtime_vars(
|
||||||
|
ctx: typer.Context,
|
||||||
|
callback: Callable,
|
||||||
|
pdb: bool = False, # --pdb
|
||||||
|
ll: Annotated[
|
||||||
|
str,
|
||||||
|
typer.Option(
|
||||||
|
'--loglevel',
|
||||||
|
'-l',
|
||||||
|
help='BigD logging level',
|
||||||
|
),
|
||||||
|
] = 'cancel', # -l info
|
||||||
|
):
|
||||||
|
'''
|
||||||
|
Maybe engage crash handling with `pdbp` when code inside
|
||||||
|
a `typer` CLI endpoint cmd raises.
|
||||||
|
|
||||||
|
To use this callback simply take your `app = typer.Typer()` instance
|
||||||
|
and decorate this function with it like so:
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
from tractor.devx import cli
|
||||||
|
|
||||||
|
app = typer.Typer()
|
||||||
|
|
||||||
|
# manual decoration to hook into `click`'s context system!
|
||||||
|
cli.load_runtime_vars = app.callback(
|
||||||
|
invoke_without_command=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
And then you can use the now augmented `click` CLI context as so,
|
||||||
|
|
||||||
|
.. code:: python
|
||||||
|
|
||||||
|
@app.command(
|
||||||
|
context_settings={
|
||||||
|
"allow_extra_args": True,
|
||||||
|
"ignore_unknown_options": True,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
def my_cli_cmd(
|
||||||
|
ctx: typer.Context,
|
||||||
|
):
|
||||||
|
rtvars: dict = ctx.runtime_vars
|
||||||
|
pdb: bool = rtvars['pdb']
|
||||||
|
|
||||||
|
with tractor.devx.cli.maybe_open_crash_handler(pdb=pdb):
|
||||||
|
trio.run(
|
||||||
|
partial(
|
||||||
|
my_tractor_main_task_func,
|
||||||
|
debug_mode=pdb,
|
||||||
|
loglevel=rtvars['ll'],
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
which will enable log level and debug mode globally for the entire
|
||||||
|
`tractor` + `trio` runtime thereafter!
|
||||||
|
|
||||||
|
Bo
|
||||||
|
|
||||||
|
'''
|
||||||
|
global _runtime_vars
|
||||||
|
_runtime_vars |= {
|
||||||
|
'pdb': pdb,
|
||||||
|
'll': ll,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.runtime_vars: dict[str, Any] = _runtime_vars
|
||||||
|
print(
|
||||||
|
f'`typer` sub-cmd: {ctx.invoked_subcommand}\n'
|
||||||
|
f'`tractor` runtime vars: {_runtime_vars}'
|
||||||
|
)
|
||||||
|
|
||||||
|
# XXX NOTE XXX: hackzone.. if no sub-cmd is specified (the
|
||||||
|
# default if the user just invokes `bigd`) then we simply
|
||||||
|
# invoke the sole `_bigd()` cmd passing in the "parent"
|
||||||
|
# typer.Context directly to that call since we're treating it
|
||||||
|
# as a "non sub-command" or wtv..
|
||||||
|
# TODO: ideally typer would have some kinda built-in way to get
|
||||||
|
# this behaviour without having to construct and manually
|
||||||
|
# invoke our own cmd..
|
||||||
|
if (
|
||||||
|
ctx.invoked_subcommand is None
|
||||||
|
or ctx.invoked_subcommand == callback.__name__
|
||||||
|
):
|
||||||
|
cmd: typer.core.TyperCommand = typer.core.TyperCommand(
|
||||||
|
name='bigd',
|
||||||
|
callback=callback,
|
||||||
|
)
|
||||||
|
ctx.params = {'ctx': ctx}
|
||||||
|
cmd.invoke(ctx)
|
|
@ -28,7 +28,6 @@ from typing import (
|
||||||
Callable,
|
Callable,
|
||||||
AsyncIterator,
|
AsyncIterator,
|
||||||
Awaitable,
|
Awaitable,
|
||||||
Optional,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
import trio
|
import trio
|
||||||
|
@ -65,9 +64,9 @@ class LinkedTaskChannel(trio.abc.Channel):
|
||||||
_trio_exited: bool = False
|
_trio_exited: bool = False
|
||||||
|
|
||||||
# set after ``asyncio.create_task()``
|
# set after ``asyncio.create_task()``
|
||||||
_aio_task: Optional[asyncio.Task] = None
|
_aio_task: asyncio.Task | None = None
|
||||||
_aio_err: Optional[BaseException] = None
|
_aio_err: BaseException | None = None
|
||||||
_broadcaster: Optional[BroadcastReceiver] = None
|
_broadcaster: BroadcastReceiver | None = None
|
||||||
|
|
||||||
async def aclose(self) -> None:
|
async def aclose(self) -> None:
|
||||||
await self._from_aio.aclose()
|
await self._from_aio.aclose()
|
||||||
|
@ -188,7 +187,7 @@ def _run_asyncio_task(
|
||||||
|
|
||||||
cancel_scope = trio.CancelScope()
|
cancel_scope = trio.CancelScope()
|
||||||
aio_task_complete = trio.Event()
|
aio_task_complete = trio.Event()
|
||||||
aio_err: Optional[BaseException] = None
|
aio_err: BaseException | None = None
|
||||||
|
|
||||||
chan = LinkedTaskChannel(
|
chan = LinkedTaskChannel(
|
||||||
aio_q, # asyncio.Queue
|
aio_q, # asyncio.Queue
|
||||||
|
@ -270,7 +269,7 @@ def _run_asyncio_task(
|
||||||
'''
|
'''
|
||||||
nonlocal chan
|
nonlocal chan
|
||||||
aio_err = chan._aio_err
|
aio_err = chan._aio_err
|
||||||
task_err: Optional[BaseException] = None
|
task_err: BaseException | None = None
|
||||||
|
|
||||||
# only to avoid ``asyncio`` complaining about uncaptured
|
# only to avoid ``asyncio`` complaining about uncaptured
|
||||||
# task exceptions
|
# task exceptions
|
||||||
|
@ -350,11 +349,11 @@ async def translate_aio_errors(
|
||||||
'''
|
'''
|
||||||
trio_task = trio.lowlevel.current_task()
|
trio_task = trio.lowlevel.current_task()
|
||||||
|
|
||||||
aio_err: Optional[BaseException] = None
|
aio_err: BaseException | None = None
|
||||||
|
|
||||||
# TODO: make thisi a channel method?
|
# TODO: make thisi a channel method?
|
||||||
def maybe_raise_aio_err(
|
def maybe_raise_aio_err(
|
||||||
err: Optional[Exception] = None
|
err: Exception | None = None
|
||||||
) -> None:
|
) -> None:
|
||||||
aio_err = chan._aio_err
|
aio_err = chan._aio_err
|
||||||
if (
|
if (
|
||||||
|
|
Loading…
Reference in New Issue