def __anext__(self):
# Read data from the socket. When we have enough data to parse, parse
# it and return the message. Until then keep reading from stream
while True:
try:
if self.buffer:
message = self.parse()
if message:
return message
logger.debug('I m stuck at reading from socket')
data = await self.reader.read(
PeerStreamIterator.CHUNK_SIZE)
if data:
self.buffer += data
message = self.parse()
if message:
return message
except ConnectionResetError:
logging.debug('Connection closed by peer')
raise StopAsyncIteration()
except CancelledError:
raise StopAsyncIteration()
except StopAsyncIteration as e:
# Cath to stop logging
raise e
except Exception:
logging.exception('Error when iterating over stream!')
raise StopAsyncIteration()
raise StopAsyncIteration()
评论列表
文章目录