Closed mkoeppe closed 11 months ago
the diff looks wild because of the indent-change
looks good : )
if you have too much time (hah...) you could split it into 2 commits:
py: use signal context
and py: add option use_cysignals
otherwise, feel free to merge
would it make sense to automatically enable cysignals
when available?
something like
try:
from cysignals import changesignal
self._log("using cysignals.changesignal")
self._changesignal = changesignal
except ModuleNotFoundError:
pass
i guess that cysignals
has no significant downsides compared to signals
would it make sense to automatically enable
cysignals
when available?
I can make that a third, default option.
you could split it into 2 commits:
done
I can make that a third, default option.
done in 32e735b
I'll merge it and cut a new release
thanks : )
for the record: the messing with signals is needed to get a "read with timeout"
ideally, python's f.read would allow
with open("/dev/stdin") as f:
text = f.read(timeout=5)
but that fails with
TypeError: TextIOWrapper.read() takes no keyword arguments
also python's input
function has no timeout
parameter
in javascript im using child_process.spawnSync to "read with timeout"
(calling dd
which only works on linux...)
const reader = child_process.spawnSync('dd', ddArgs, {
timeout,
stdio,
windowsHide: true,
...options,
});
so in python we could use subprocess.run
#!/usr/bin/env python3
import sys
import subprocess
fd_read = 0 # stdin # TODO use the jobserver's fd_read
# pipe one byte from fd_read to stdout
py_code = f"""
import sys; sys.stdout.buffer.write(open({fd_read}, "rb").read(1))
"""
args = [
sys.executable, # python
"-c", py_code,
]
try:
input_byte = subprocess.run(
args,
capture_output=True,
timeout=2,
).stdout
except subprocess.TimeoutExpired:
input_byte = None
print("input_byte", input_byte)
$ ./test-read.py
input_byte None
$ echo xy | ./test-read.py
input_byte b'x'
Thanks for the explanation! For now, I think, the solution with signals works well though
SageMath uses https://github.com/sagemath/cysignals for advanced interrupt and signal handling with Cython modules.
We add an option
JobClient(use_cysignals=True)
(default:False
). With the option set, it imports a context managerchangesignal
fromcysignals
, which replaces the use of the standard library functionsignals.signal
.This new option helps us solve https://github.com/sagemath/sage/issues/36944