2025-09-07 22:47:10 +00:00
|
|
|
from contextlib import (
|
|
|
|
asynccontextmanager as acm,
|
|
|
|
)
|
2025-09-06 18:03:02 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
import tractor
|
|
|
|
import trio
|
|
|
|
|
|
|
|
|
|
|
|
log = tractor.log.get_logger(
|
|
|
|
name=__name__
|
|
|
|
)
|
|
|
|
|
2025-09-07 22:47:10 +00:00
|
|
|
_lock: trio.Lock|None = None
|
|
|
|
|
2025-09-06 18:03:02 +00:00
|
|
|
|
2025-09-07 22:47:10 +00:00
|
|
|
@acm
|
2025-09-06 18:03:02 +00:00
|
|
|
async def acquire_singleton_lock(
|
|
|
|
) -> None:
|
2025-09-07 22:47:10 +00:00
|
|
|
global _lock
|
|
|
|
if _lock is None:
|
|
|
|
log.info('Allocating LOCK')
|
|
|
|
_lock = trio.Lock()
|
|
|
|
|
2025-09-06 18:03:02 +00:00
|
|
|
log.info('TRYING TO LOCK ACQUIRE')
|
2025-09-07 22:47:10 +00:00
|
|
|
async with _lock:
|
|
|
|
log.info('ACQUIRED')
|
|
|
|
yield _lock
|
2025-09-06 18:03:02 +00:00
|
|
|
|
2025-09-07 22:47:10 +00:00
|
|
|
log.info('RELEASED')
|
2025-09-06 18:03:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-07 22:47:10 +00:00
|
|
|
async def hold_lock_forever(
|
|
|
|
task_status=trio.TASK_STATUS_IGNORED
|
|
|
|
):
|
|
|
|
async with (
|
|
|
|
tractor.trionics.maybe_raise_from_masking_exc(),
|
|
|
|
acquire_singleton_lock() as lock,
|
|
|
|
):
|
|
|
|
task_status.started(lock)
|
|
|
|
await trio.sleep_forever()
|
2025-09-06 18:03:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def main(
|
|
|
|
ignore_special_cases: bool,
|
|
|
|
loglevel: str = 'info',
|
|
|
|
debug_mode: bool = True,
|
|
|
|
):
|
2025-09-07 22:47:10 +00:00
|
|
|
async with (
|
|
|
|
trio.open_nursery() as tn,
|
|
|
|
|
|
|
|
# tractor.trionics.maybe_raise_from_masking_exc()
|
|
|
|
# ^^^ XXX NOTE, interestingly putting the unmasker
|
|
|
|
# here does not exhibit the same behaviour ??
|
|
|
|
):
|
|
|
|
if not ignore_special_cases:
|
|
|
|
from tractor.trionics import _taskc
|
|
|
|
_taskc._mask_cases.clear()
|
|
|
|
|
|
|
|
_lock = await tn.start(
|
|
|
|
hold_lock_forever,
|
|
|
|
)
|
|
|
|
with trio.move_on_after(0.2):
|
|
|
|
await tn.start(
|
|
|
|
hold_lock_forever,
|
2025-09-06 18:03:02 +00:00
|
|
|
)
|
|
|
|
|
2025-09-07 22:47:10 +00:00
|
|
|
tn.cancel_scope.cancel()
|
2025-09-06 18:03:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
# XXX, manual test as script
|
|
|
|
if __name__ == '__main__':
|
|
|
|
tractor.log.get_console_log(level='info')
|
2025-09-07 22:47:10 +00:00
|
|
|
for case in [True, False]:
|
2025-09-06 18:03:02 +00:00
|
|
|
log.info(
|
|
|
|
f'\n'
|
|
|
|
f'------ RUNNING SCRIPT TRIAL ------\n'
|
2025-09-07 22:47:10 +00:00
|
|
|
f'ignore_special_cases: {case!r}\n'
|
2025-09-06 18:03:02 +00:00
|
|
|
)
|
|
|
|
trio.run(partial(
|
|
|
|
main,
|
|
|
|
ignore_special_cases=case,
|
|
|
|
loglevel='info',
|
|
|
|
))
|