2018-06-07 04:29:17 +00:00
|
|
|
"""
|
2018-09-03 02:07:32 +00:00
|
|
|
Spawning basics
|
2018-06-07 04:29:17 +00:00
|
|
|
"""
|
2020-12-21 14:09:55 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2019-03-24 00:29:37 +00:00
|
|
|
import pytest
|
2018-06-12 19:23:58 +00:00
|
|
|
import trio
|
2018-07-05 23:49:21 +00:00
|
|
|
import tractor
|
2018-06-07 04:29:17 +00:00
|
|
|
|
2018-08-07 18:30:25 +00:00
|
|
|
from conftest import tractor_test
|
2018-07-10 21:19:54 +00:00
|
|
|
|
2020-12-21 14:09:55 +00:00
|
|
|
data_to_pass_down = {'doggy': 10, 'kitty': 4}
|
2018-07-11 23:24:37 +00:00
|
|
|
|
|
|
|
|
2020-12-21 14:09:55 +00:00
|
|
|
async def spawn(is_arbiter, data):
|
2018-07-06 06:45:26 +00:00
|
|
|
namespaces = [__name__]
|
2018-06-19 15:49:25 +00:00
|
|
|
|
|
|
|
await trio.sleep(0.1)
|
|
|
|
actor = tractor.current_actor()
|
|
|
|
assert actor.is_arbiter == is_arbiter
|
2020-12-21 14:09:55 +00:00
|
|
|
data == data_to_pass_down
|
2018-06-19 15:49:25 +00:00
|
|
|
|
|
|
|
if actor.is_arbiter:
|
|
|
|
async with tractor.open_nursery() as nursery:
|
|
|
|
# forks here
|
2018-08-02 19:27:09 +00:00
|
|
|
portal = await nursery.run_in_actor(
|
|
|
|
spawn,
|
|
|
|
is_arbiter=False,
|
2020-12-21 14:09:55 +00:00
|
|
|
name='sub-actor',
|
|
|
|
data=data,
|
2018-06-19 15:49:25 +00:00
|
|
|
rpc_module_paths=namespaces,
|
|
|
|
)
|
2018-08-02 19:27:09 +00:00
|
|
|
|
2018-06-19 15:49:25 +00:00
|
|
|
assert len(nursery._children) == 1
|
2018-07-05 19:33:02 +00:00
|
|
|
assert portal.channel.uid in tractor.current_actor()._peers
|
2018-07-08 02:22:05 +00:00
|
|
|
# be sure we can still get the result
|
2018-07-06 06:45:26 +00:00
|
|
|
result = await portal.result()
|
|
|
|
assert result == 10
|
|
|
|
return result
|
2018-06-19 15:49:25 +00:00
|
|
|
else:
|
|
|
|
return 10
|
|
|
|
|
|
|
|
|
2018-08-07 18:30:25 +00:00
|
|
|
def test_local_arbiter_subactor_global_state(arb_addr):
|
2018-07-06 06:45:26 +00:00
|
|
|
result = tractor.run(
|
2020-12-21 14:09:55 +00:00
|
|
|
partial(spawn, data=data_to_pass_down),
|
2018-06-19 15:49:25 +00:00
|
|
|
True,
|
|
|
|
name='arbiter',
|
2018-08-07 18:30:25 +00:00
|
|
|
arbiter_addr=arb_addr,
|
2018-06-19 15:49:25 +00:00
|
|
|
)
|
2018-07-06 06:45:26 +00:00
|
|
|
assert result == 10
|
|
|
|
|
2018-06-12 19:23:58 +00:00
|
|
|
|
2021-04-27 13:14:08 +00:00
|
|
|
async def movie_theatre_question():
|
2018-07-10 21:19:54 +00:00
|
|
|
"""A question asked in a dark theatre, in a tangent
|
|
|
|
(errr, I mean different) process.
|
|
|
|
"""
|
|
|
|
return 'have you ever seen a portal?'
|
|
|
|
|
|
|
|
|
|
|
|
@tractor_test
|
2019-03-09 01:06:16 +00:00
|
|
|
async def test_movie_theatre_convo(start_method):
|
2018-07-10 21:19:54 +00:00
|
|
|
"""The main ``tractor`` routine.
|
|
|
|
"""
|
|
|
|
async with tractor.open_nursery() as n:
|
2018-08-03 04:55:50 +00:00
|
|
|
|
2018-07-10 21:19:54 +00:00
|
|
|
portal = await n.start_actor(
|
|
|
|
'frank',
|
|
|
|
# enable the actor to run funcs from this current module
|
|
|
|
rpc_module_paths=[__name__],
|
|
|
|
)
|
|
|
|
|
2020-12-22 15:35:05 +00:00
|
|
|
print(await portal.run(movie_theatre_question))
|
2018-08-03 04:55:50 +00:00
|
|
|
# call the subactor a 2nd time
|
2020-12-22 15:35:05 +00:00
|
|
|
print(await portal.run(movie_theatre_question))
|
2018-07-10 21:19:54 +00:00
|
|
|
|
|
|
|
# the async with will block here indefinitely waiting
|
2018-08-02 19:27:09 +00:00
|
|
|
# for our actor "frank" to complete, we cancel 'frank'
|
|
|
|
# to avoid blocking indefinitely
|
2018-07-10 21:19:54 +00:00
|
|
|
await portal.cancel_actor()
|
|
|
|
|
|
|
|
|
2021-04-27 13:14:08 +00:00
|
|
|
async def cellar_door():
|
2018-07-10 21:19:54 +00:00
|
|
|
return "Dang that's beautiful"
|
|
|
|
|
|
|
|
|
|
|
|
@tractor_test
|
2019-03-09 01:06:16 +00:00
|
|
|
async def test_most_beautiful_word(start_method):
|
2018-07-10 21:19:54 +00:00
|
|
|
"""The main ``tractor`` routine.
|
|
|
|
"""
|
|
|
|
async with tractor.open_nursery() as n:
|
2018-08-03 04:55:50 +00:00
|
|
|
|
2020-12-21 14:09:55 +00:00
|
|
|
portal = await n.run_in_actor(
|
|
|
|
cellar_door,
|
|
|
|
name='some_linguist',
|
|
|
|
)
|
2018-07-10 21:19:54 +00:00
|
|
|
|
|
|
|
# The ``async with`` will unblock here since the 'some_linguist'
|
|
|
|
# actor has completed its main task ``cellar_door``.
|
|
|
|
|
|
|
|
print(await portal.result())
|
2019-03-24 00:29:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def check_loglevel(level):
|
|
|
|
assert tractor.current_actor().loglevel == level
|
|
|
|
log = tractor.log.get_logger()
|
|
|
|
# XXX using a level actually used inside tractor seems to trigger
|
|
|
|
# some kind of `logging` module bug FYI.
|
|
|
|
log.critical('yoyoyo')
|
|
|
|
|
|
|
|
|
|
|
|
def test_loglevel_propagated_to_subactor(
|
|
|
|
start_method,
|
|
|
|
capfd,
|
|
|
|
arb_addr,
|
|
|
|
):
|
|
|
|
if start_method == 'forkserver':
|
|
|
|
pytest.skip(
|
|
|
|
"a bug with `capfd` seems to make forkserver capture not work?")
|
|
|
|
|
|
|
|
level = 'critical'
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
async with tractor.open_nursery() as tn:
|
|
|
|
await tn.run_in_actor(
|
|
|
|
check_loglevel,
|
|
|
|
level=level,
|
|
|
|
)
|
|
|
|
|
|
|
|
tractor.run(
|
|
|
|
main,
|
|
|
|
name='arbiter',
|
|
|
|
loglevel=level,
|
|
|
|
start_method=start_method,
|
|
|
|
arbiter_addr=arb_addr,
|
|
|
|
)
|
|
|
|
# ensure subactor spits log message on stderr
|
|
|
|
captured = capfd.readouterr()
|
|
|
|
assert 'yoyoyo' in captured.err
|