#!/usr/bin/python3.7 import asyncio def get_future(n): fut = asyncio.Future() # Set fut done after n seconds asyncio.ensure_future(asyncio.sleep(n)).add_done_callback(fut.set_result) return fut async def task(): while True: fut = get_future(2) waiter = asyncio.ensure_future(asyncio.wait_for(fut, 3)) try: await asyncio.shield(waiter) except asyncio.CancelledError: print('Cancelled; continue waiting') await waiter break async def killer(fut): await asyncio.sleep(1) fut.cancel() async def main(): t0 = asyncio.ensure_future(task()) t1 = asyncio.ensure_future(killer(t0)) await t0 asyncio.run(main())