async def test_get_running_tasks() -> None: async def inspect() -> None: await wait_all_tasks_blocked() new_tasks = set(get_running_tasks()) - existing_tasks task_infos[:] = sorted(new_tasks, key=lambda info: info.name or "") event.set() event = Event() task_infos: List[TaskInfo] = [] host_task = get_current_task() async with create_task_group() as tg: existing_tasks = set(get_running_tasks()) tg.start_soon(event.wait, name="task1") tg.start_soon(event.wait, name="task2") tg.start_soon(inspect) assert len(task_infos) == 3 expected_names = [ "task1", "task2", "tests.test_debugging.test_get_running_tasks.<locals>.inspect", ] for task, expected_name in zip(task_infos, expected_names): assert task.parent_id == host_task.id assert task.name == expected_name assert repr(task) == f"TaskInfo(id={task.id}, name={expected_name!r})"
def task_wrap(): assert anyio.get_current_task().id == outer_task_id async def corofn(): nonlocal inner_task_id inner_task_id = anyio.get_current_task().id return corofn()
def task_wrap() -> Coroutine[object, object, None]: assert anyio.get_current_task().id == outer_task_id async def corofn() -> None: nonlocal inner_task_id inner_task_id = anyio.get_current_task().id return corofn()
async def test_start_soon_parent_id() -> None: root_task_id = get_current_task().id parent_id: Optional[int] = None async def subtask() -> None: nonlocal parent_id parent_id = get_current_task().parent_id async def starter_task() -> None: tg.start_soon(subtask) async with anyio.create_task_group() as tg: tg.start_soon(starter_task) assert parent_id == root_task_id
async def test_task_in_sync_spawn_callback(): outer_task_id = anyio.get_current_task().id inner_task_id = None def task_wrap(): assert anyio.get_current_task().id == outer_task_id async def corofn(): nonlocal inner_task_id inner_task_id = anyio.get_current_task().id return corofn() async with create_task_group() as tg: tg.spawn(task_wrap) assert inner_task_id is not None assert inner_task_id != outer_task_id
async def test_task_in_sync_spawn_callback() -> None: outer_task_id = anyio.get_current_task().id inner_task_id = None def task_wrap() -> Coroutine[object, object, None]: assert anyio.get_current_task().id == outer_task_id async def corofn() -> None: nonlocal inner_task_id inner_task_id = anyio.get_current_task().id return corofn() async with create_task_group() as tg: tg.start_soon(task_wrap) assert inner_task_id is not None assert inner_task_id != outer_task_id
async def test_get_running_tasks(): async def inspect(): await wait_all_tasks_blocked() new_tasks = set(get_running_tasks()) - existing_tasks task_infos[:] = sorted(new_tasks, key=lambda info: info.name or '') event.set() event = create_event() task_infos = [] host_task = get_current_task() async with create_task_group() as tg: existing_tasks = set(get_running_tasks()) tg.spawn(event.wait, name='task1') tg.spawn(event.wait, name='task2') tg.spawn(inspect) assert len(task_infos) == 3 expected_names = ['task1', 'task2', 'test_debugging.test_get_running_tasks.<locals>.inspect'] for task, expected_name in zip(task_infos, expected_names): assert task.parent_id == host_task.id assert task.name == expected_name assert repr(task) == f'TaskInfo(id={task.id}, name={expected_name!r})'
async def test_start_parent_id() -> None: root_task_id = get_current_task().id starter_task_id: Optional[int] = None initial_parent_id: Optional[int] = None permanent_parent_id: Optional[int] = None async def subtask(*, task_status: TaskStatus) -> None: nonlocal initial_parent_id, permanent_parent_id initial_parent_id = get_current_task().parent_id task_status.started() permanent_parent_id = get_current_task().parent_id async def starter_task() -> None: nonlocal starter_task_id starter_task_id = get_current_task().id await tg.start(subtask) async with anyio.create_task_group() as tg: tg.start_soon(starter_task) assert initial_parent_id != permanent_parent_id assert initial_parent_id == starter_task_id assert permanent_parent_id == root_task_id
async def non_main(*, task_status: TaskStatus) -> None: task_status.started(anyio.get_current_task().name)
async def main() -> None: nonlocal task_name task_name = get_current_task().name
async def test_get_current_task(self) -> None: task = await maybe_async(get_current_task()) assert type(task) is TaskInfo
def taskfunc(*, task_status): task_status.started(get_current_task().name)
async def taskfunc(): nonlocal task_name task_name = get_current_task().name
def taskfunc(*, task_status: TaskStatus) -> None: task_status.started(get_current_task().name)
async def corofn() -> None: nonlocal inner_task_id inner_task_id = anyio.get_current_task().id
async def starter_task() -> None: nonlocal starter_task_id starter_task_id = get_current_task().id await tg.start(subtask)
async def subtask(*, task_status: TaskStatus) -> None: nonlocal initial_parent_id, permanent_parent_id initial_parent_id = get_current_task().parent_id task_status.started() permanent_parent_id = get_current_task().parent_id
async def subtask() -> None: nonlocal parent_id parent_id = get_current_task().parent_id