2024-03-20 23:13:13 +00:00
|
|
|
import trio
|
|
|
|
import tractor
|
|
|
|
|
|
|
|
|
2024-03-22 20:41:49 +00:00
|
|
|
def sync_pause(
|
|
|
|
use_builtin: bool = True,
|
|
|
|
error: bool = False,
|
|
|
|
):
|
|
|
|
if use_builtin:
|
2024-05-28 13:22:59 +00:00
|
|
|
breakpoint(hide_tb=False)
|
2024-03-22 20:41:49 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
tractor.pause_from_sync()
|
|
|
|
|
|
|
|
if error:
|
|
|
|
raise RuntimeError('yoyo sync code error')
|
2024-03-20 23:13:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@tractor.context
|
|
|
|
async def start_n_sync_pause(
|
|
|
|
ctx: tractor.Context,
|
|
|
|
):
|
2024-05-28 13:22:59 +00:00
|
|
|
actor: tractor.Actor = tractor.current_actor()
|
|
|
|
|
|
|
|
# sync to parent-side task
|
2024-03-20 23:13:13 +00:00
|
|
|
await ctx.started()
|
|
|
|
|
|
|
|
print(f'entering SYNC PAUSE in {actor.uid}')
|
|
|
|
sync_pause()
|
|
|
|
print(f'back from SYNC PAUSE in {actor.uid}')
|
|
|
|
|
|
|
|
|
|
|
|
async def main() -> None:
|
|
|
|
async with tractor.open_nursery(
|
2024-05-28 13:22:59 +00:00
|
|
|
# NOTE: required for pausing from sync funcs
|
|
|
|
maybe_enable_greenback=True,
|
2024-03-20 23:13:13 +00:00
|
|
|
debug_mode=True,
|
|
|
|
) as an:
|
|
|
|
|
|
|
|
p: tractor.Portal = await an.start_actor(
|
|
|
|
'subactor',
|
|
|
|
enable_modules=[__name__],
|
|
|
|
# infect_asyncio=True,
|
|
|
|
debug_mode=True,
|
|
|
|
loglevel='cancel',
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO: 3 sub-actor usage cases:
|
|
|
|
# -[ ] via a `.run_in_actor()` call
|
|
|
|
# -[ ] via a `.run()`
|
|
|
|
# -[ ] via a `.open_context()`
|
|
|
|
#
|
|
|
|
async with p.open_context(
|
|
|
|
start_n_sync_pause,
|
|
|
|
) as (ctx, first):
|
|
|
|
assert first is None
|
|
|
|
|
|
|
|
await tractor.pause()
|
|
|
|
sync_pause()
|
|
|
|
|
|
|
|
# TODO: make this work!!
|
|
|
|
await trio.to_thread.run_sync(
|
|
|
|
sync_pause,
|
|
|
|
abandon_on_cancel=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
await ctx.cancel()
|
|
|
|
|
|
|
|
# TODO: case where we cancel from trio-side while asyncio task
|
|
|
|
# has debugger lock?
|
|
|
|
await p.cancel_actor()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
trio.run(main)
|