异步上下文管理器
class AsyncDatabase: async def __aenter__(self): await self.connect() return self async def __aexit__(self, exc_type, exc_val, exc_tb): await self.close()
任务管理
async def main(): tasks = [asyncio.create_task(fetch_data(i)) for i in range(5)] results = await asyncio.gather(*tasks, return_exceptions=True) return results
转载请注明:周志洋的博客 » Python进阶-异步编程进阶