周志洋

个人站

持续学习 才能不被淘汰


Python进阶-异步编程进阶

异步上下文管理器

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进阶-异步编程进阶

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦