设置协程超时时间
import asyncio import async_timeout async def main(): try: async with async_timeout.timeout(1): await asyncio.sleep(1) # io操作 print('协程执行完成') except asyncio.TimeoutError: print('协程执行超时') if __name__ == '__main__': asyncio.run(main())
import asyncio import async_timeout async def main(): try: async with async_timeout.timeout(1): await asyncio.sleep(1) # io操作 print('协程执行完成') except asyncio.TimeoutError: print('协程执行超时') if __name__ == '__main__': asyncio.run(main())