12345678910111213141516171819202122 |
- # -*- coding: utf-8 -*-
- # @Time : 2023/7/20 16:10
- # @Author : XuJiakai
- # @File : async_pool
- # @Software: PyCharm
- import asyncio
- from typing import Coroutine
- class AsyncPool(object):
- def __init__(self, max_concurrency: int):
- self._semaphore: asyncio.Semaphore = asyncio.Semaphore(max_concurrency)
- async def create_task(self, coro: Coroutine) -> asyncio.Task:
- await self._semaphore.acquire()
- task: asyncio.Task = asyncio.create_task(coro)
- task.add_done_callback(lambda t: self._semaphore.release())
- return task
- if __name__ == '__main__':
- pass
|