class Request(object):
def __init__(self, url: str, host: str, port: int, session: ClientSession, data: bytes,
ids_to_remove: list, callback):
super().__init__()
self.__url = url
self.__host = host
self.__port = port
self.__session = session
self.__data = data
self.__ids_to_remove = ids_to_remove
self.__callback = callback
async def send(self):
try:
async with self.__session.post('http://{}:{}{}'.format(self.__host, self.__port, self.__url),
data=self.__data) as r:
if r.status == int(HTTPStatus.OK):
await self.__callback(self.__ids_to_remove)
except Exception as e:
print(e)
def ids_to_remove(self):
return self.__ids_to_remove
class ConnectionsPool(object):
def __init__(self, host: str, port: int, max_connections: int, remove_ids_callback):
super().__init__()
self.__max_connections = max_connections
self.__host = host
self.__port = port
self.__remove_ids_callback = remove_ids_callback
self.__sending_futures = []
self.__session = ClientSession(connector=TCPConnector(verify_ssl=False, limit=max_connections))
async def send(self, url: str, data: bytes, ids_to_remove: List):
if len(self.__sending_futures) > self.__max_connections:
await asyncio.wait(self.__sending_futures, return_when=asyncio.FIRST_COMPLETED)
request = Request(url, self.__host, self.__port, self.__session, data, ids_to_remove,
self.__remove_ids_callback)
future = asyncio.ensure_future(request.send())
future.request = request
future.add_done_callback(self.future_done)
self.__sending_futures.append(future)
def future_done(self, future):
self.__sending_futures.remove(future)
def ids_to_remove(self):
result = []
for f in self.__sending_futures:
result += f.request.ids_to_remove()
return result
root: 'Task' object has no attribute 'ids_to_remove' (abstract_retranslator.py:31)
Traceback (most recent call last):
File "/srv/backend/common/retranslation/abstract_retranslator.py", line 29, in retranslate
await self._retranslate()
File "/srv/backend/common/retranslation/http_retranslator.py", line 135, in _retranslate
async for event in self.events(connections_pool.ids_to_remove()):
File "/srv/backend/common/retranslation/http_retranslator.py", line 70, in ids_to_remove
result += f.request.ids_to_remove()
AttributeError: 'Task' object has no attribute 'ids_to_remove'
Как f.request может стать Task? Или я неправильно готовлю асинхронный код на Пайтоне?
python 3.5