# async_cancel_task2.py
import asyncio


async def task_func():
    print("in task_func, sleeping")
    try:
        await asyncio.sleep(1)
    except asyncio.CancelledError:
        print("task_func was canceled")
        raise
    return "the result"


def task_canceller(t: asyncio.Task):
    print("in task_canceller")
    t.cancel()
    print("canceled tthe task")


async def main(loop: asyncio.AbstractEventLoop):
    print("creating task")
    task = loop.create_task(task_func())
    loop.call_soon(task_canceller, task)
    try:
        await task
    except asyncio.CancelledError:
        print("main() also sees task as canceled")


event_loop = asyncio.get_event_loop()
try:
    event_loop.run_until_complete(main(event_loop))
finally:
    event_loop.close()
