Skip to content

Commit

Permalink
Merge pull request #62 from Kripner/main
Browse files Browse the repository at this point in the history
asyncio/pexpect fixes, small improvements
  • Loading branch information
lenianiva authored Jan 26, 2025
2 parents 78b05ec + 2ba5033 commit f97bc49
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
26 changes: 17 additions & 9 deletions pantograph/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def __init__(self,
# Set `{ "automaticMode" : False }` to handle resumption by yourself.
options: Dict[str, Any]={},
core_options: List[str]=DEFAULT_CORE_OPTIONS,
timeout: int=120,
timeout: int=30,
maxread: int=1000000,
_sync_init: bool=True):
"""
timeout: Amount of time to wait for execution
timeout: Amount of time to wait for execution (in seconds)
maxread: Maximum number of characters to read (especially important for large proofs and catalogs)
"""
self.timeout = timeout
Expand Down Expand Up @@ -93,7 +93,7 @@ async def create(cls,
maxread: int=1000000,
start:bool=True) -> 'Server':
"""
timeout: Amount of time to wait for execution
timeout: Amount of time to wait for execution (in seconds)
maxread: Maximum number of characters to read (especially important for large proofs and catalogs)
"""
self = cls(
Expand Down Expand Up @@ -163,19 +163,19 @@ async def restart_async(self):
raise RuntimeError("Server failed to emit ready signal in time") from exc

if self.options:
await self.run_async("options.set", self.options)
await self.run_async("options.set", self.options, assert_no_error=True)

await self.run_async('options.set', {'printDependentMVars': True})
await self.run_async('options.set', {'printDependentMVars': True}, assert_no_error=True)

restart = to_sync(restart_async)

async def run_async(self, cmd, payload):
async def run_async(self, cmd, payload, assert_no_error=False):
"""
Runs a raw JSON command. Preferably use one of the commands below.
:meta private:
"""
assert self.proc
assert self.proc, "Server not running."
s = json.dumps(payload, ensure_ascii=False)
await self.proc.sendline_async(f"{cmd} {s}")
try:
Expand All @@ -185,15 +185,23 @@ async def run_async(self, cmd, payload):
if obj.get("error") == "io":
# The server is dead
self._close()
if "error" in obj and assert_no_error:
raise ServerError(obj)
return obj
except Exception as e:
if self.proc.closed:
raise ServerError(f"Cannot decode: '{line}'") from e
self.proc.sendeof()
remainder = await self.proc.read_async()
self._close()
raise ServerError(f"Cannot decode: {line}\n{remainder}") from e
raise ServerError(f"Cannot decode: '{line}\n{remainder}'") from e
except pexpect.exceptions.TIMEOUT as exc:
self._close()
return {"error": "timeout", "message": str(exc)}
result = {"error": "timeout", "message": str(exc)}
if assert_no_error:
raise ServerError(result) from exc
else:
return result

run = to_sync(run_async)

Expand Down
13 changes: 10 additions & 3 deletions pantograph/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
def to_sync(func):
@F.wraps(func)
def wrapper(*args, **kwargs):
return asyncio.get_event_loop().run_until_complete(func(*args, **kwargs))
return asyncio.run(func(*args, **kwargs))
return wrapper

async def check_output(*args, **kwargs):
Expand Down Expand Up @@ -83,15 +83,22 @@ async def readline_async(self, size=-1):
This looks for a newline as a CR/LF pair (\\r\\n) even on UNIX because
this is what the pseudotty device returns. So contrary to what you may
expect you will receive newlines as \\r\\n.
See https://pexpect.readthedocs.io/en/latest/overview.html#find-the-end-of-line-cr-lf-conventions
for more information.
If the size argument is 0 then an empty string is returned. In all
other cases the size argument is ignored, which is not standard
behavior for a file-like object. '''

if size == 0:
return self.string_type()
# delimiter default is EOF
index = await self.expect([self.crlf, self.delimiter], async_=True)

def blocking_expect():
# We use `asyncio.to_thread` because passing `async_=True` to `expect()` sometimes hangs. See e.g.:
# https://stackoverflow.com/questions/12647212/why-is-pexpect-intermittently-hanging-not-detecting-eof-after-executing-certai
return self.expect([self.crlf, self.delimiter])

index = await asyncio.to_thread(blocking_expect)
if index == 0:
return self.before + self.crlf
else:
Expand Down

0 comments on commit f97bc49

Please sign in to comment.