import asyncioimport timeasync def print_hello(): """ 异步任务:每分钟打印一次"你好" """ while True: print("你好") # 打印你好 await asyncio.sleep(1) # 等待1秒async def main(): """ 主函数 """ print("程序开始运行,将每分钟打印一次'你好'...") print("握手") # 创建异步任务 task = asyncio.create_task(print_hello()) #异步取消 await asyncio.sleep(3) task.cancel() try: await task except asyncio.CancelledError: print("异步程序终止")if __name__ == "__main__": # 运行异步主函数 asyncio.run(main())