Conversation
|
the diff looks wild because of the indent-change diff minus indentdiff --git a/py/src/gnumake_tokenpool/tokenpool.py b/py/src/gnumake_tokenpool/tokenpool.py
index b0708ea..f67eb54 100644
--- a/py/src/gnumake_tokenpool/tokenpool.py
+++ b/py/src/gnumake_tokenpool/tokenpool.py
@@ -1,8 +1,9 @@
import sys, os, stat, select, signal, time, re
+
+from contextlib import contextmanager
from datetime import datetime
from typing import List
-
__version__ = '0.0.3'
@@ -26,6 +27,7 @@ class JobClient:
max_load: int or None = None,
debug: bool or None = None,
debug2: bool or None = None,
+ use_cysignals: bool = False,
):
self._fdRead = None
@@ -47,6 +49,10 @@ class JobClient:
self._log = self._get_log(self._debug)
self._log2 = self._get_log(self._debug2)
+ if use_cysignals:
+ from cysignals import changesignal
+ self._changesignal = changesignal
+
makeFlags = os.environ.get("MAKEFLAGS", "")
if makeFlags:
self._log(f"init: MAKEFLAGS: {makeFlags}")
@@ -181,8 +187,8 @@ class JobClient:
os.close(self._fdReadDup)
# SIGALRM = timer has fired = read timeout
- old_sigalrm_handler = signal.signal(signal.SIGALRM, read_timeout_handler)
-
+ with self._changesignal(signal.SIGALRM, read_timeout_handler):
+ try:
# Set SA_RESTART to limit EINTR occurrences.
# by default, signal.signal clears the SA_RESTART flag.
# TODO is this necessary?
@@ -206,12 +212,9 @@ class JobClient:
self._log(f"acquire: read failed: {e}")
return None # jobserver is full, try again later
raise e # unexpected error
-
+ finally:
signal.setitimer(signal.ITIMER_REAL, 0) # clear timer. unix only
- # clear signal handlers
- signal.signal(signal.SIGALRM, old_sigalrm_handler)
-
#if len(buffer) == 0:
# return None
assert len(buffer) == 1
@@ -292,3 +295,13 @@ class JobClient:
self._log(f"init failed: fd {fd} stat failed: {e}")
raise NoJobServer()
raise e # unexpected error
+
+ @staticmethod
+ @contextmanager
+ def _changesignal(sig, action):
+ old_sig_handler = signal.signal(sig, action)
+ try:
+ yield
+ finally:
+ # clear signal handler
+ signal.signal(sig, old_sig_handler)looks good : ) if you have too much time (hah...) you could split it into 2 commits: otherwise, feel free to merge |
|
would it make sense to automatically enable something like try:
from cysignals import changesignal
self._log("using cysignals.changesignal")
self._changesignal = changesignal
except ModuleNotFoundError:
passi guess that |
I can make that a third, default option. |
…), try using but do not require cysignals)
9756bf0 to
32e735b
Compare
done |
done in 32e735b |
|
I'll merge it and cut a new release |
|
thanks : ) for the record: ideally, python's f.read would allow with open("/dev/stdin") as f:
text = f.read(timeout=5)but that fails with also python's in javascript im using child_process.spawnSync to "read with timeout" 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) |
|
Thanks for the explanation! |
…mInterrupt` problems <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes sagemath#1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> The new release 0.0.4 provides cysignals integration: - milahu/gnumake-tokenpool#4 Fixes sagemath#36944 To verify that the tokenpool mechanism is being used: ``` $ DEBUG_JOBCLIENT=1 make ptest ``` <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes sagemath#12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - sagemath#12345: short description why this is a dependency - sagemath#34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: sagemath#36948 Reported by: Matthias Köppe Reviewer(s): John H. Palmieri
sagemathgh-36948: `build/pkgs/gnumake_tokenpool`: Update to fix `AlarmInterrupt` problems <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes sagemath#1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> The new release 0.0.4 provides cysignals integration: - milahu/gnumake-tokenpool#4 Fixes sagemath#36944 To verify that the tokenpool mechanism is being used: ``` $ DEBUG_JOBCLIENT=1 make ptest ``` <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes sagemath#12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - sagemath#12345: short description why this is a dependency - sagemath#34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: sagemath#36948 Reported by: Matthias Köppe Reviewer(s): John H. Palmieri
sagemathgh-36948: `build/pkgs/gnumake_tokenpool`: Update to fix `AlarmInterrupt` problems <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes sagemath#1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> The new release 0.0.4 provides cysignals integration: - milahu/gnumake-tokenpool#4 Fixes sagemath#36944 To verify that the tokenpool mechanism is being used: ``` $ DEBUG_JOBCLIENT=1 make ptest ``` <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes sagemath#12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - sagemath#12345: short description why this is a dependency - sagemath#34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: sagemath#36948 Reported by: Matthias Köppe Reviewer(s): John H. Palmieri
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 manager
changesignalfromcysignals, which replaces the use of the standard library functionsignals.signal.This new option helps us solve sagemath/sage#36944