def __iter__(self) -> Iterator[str]:
'''Return an iterator over the lines written to stdout. May only be called once! Might raise a SolverSubprocessError.'''
assert not self.iterating, 'You may only iterate once over a single DlvhexLineReader instance.'
self.iterating = True
# Requirement: dlvhex2 needs to flush stdout after every line
with io.TextIOWrapper(self.process.stdout, encoding=self.stdout_encoding) as stdout_lines:
for line in stdout_lines:
yield line
# Tell dlvhex2 to prepare the next answer set
if not self.process.stdin.closed:
self.process.stdin.write(b'\n')
self.process.stdin.flush()
else:
break
# We've exhausted stdout, so either:
# 1. we got all answer sets, or
# 2. an error occurred,
# and dlvhex closed stdout (and probably terminated).
# Give it a chance to terminate gracefully.
try:
self.process.wait(timeout=0.005) # type: ignore (mypy does not know about `timeout`)
except subprocess.TimeoutExpired: # type: ignore (mypy does not know about `TimeoutExpired`)
pass
self.close()
评论列表
文章目录