Feature or enhancement
Proposal:
As a follow up on https://discuss.python.org/t/a-lightweight-cooperative-timeout-mechanism-for-synchronous-code-with-timeout-seconds/107811
>>> with timeout(0.2):
... while True:
... pass
...
Traceback (most recent call last):
File "<python-input-1>", line 3, in <module>
pass
TimeoutError: Timeout
>>> with timeout(0.2):
... re.match(r"(a+)+b", "a" * 30 + "c")
...
Traceback (most recent call last):
File "<python-input-2>", line 2, in <module>
re.match(r"(a+)+b", "a" * 30 + "c")
~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../cpython/Lib/re/__init__.py", line 166, in prefixmatch
return _compile(pattern, flags).prefixmatch(string)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
TimeoutError: Timeout
Key Proporties:
Tested on my Macbook Pro M4
- No extra overhead outside a timeout block (i.e.
timeout_block == NULL timeout check is completely bypassed).
Py_CheckTimeOut
- fast path (with skip interval ≥ 8) takes 1.1~1.3ns per call;
- slow path (with skip interval < 8) takes 2.0~6.3ns per call;
- full calling (without skip interval) takes 10.2ns +- 1.2ns per call.
re module integration
By tapping into the existing signal‑check counter inside _sre, timeout checks add no measurable overhead to normal regexps while allowing catastrophic backtracking to be safely interrupted within the specified deadline.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/a-lightweight-cooperative-timeout-mechanism-for-synchronous-code-with-timeout-seconds/107811
Linked PRs
Feature or enhancement
Proposal:
As a follow up on https://discuss.python.org/t/a-lightweight-cooperative-timeout-mechanism-for-synchronous-code-with-timeout-seconds/107811
Key Proporties:
Tested on my Macbook Pro M4
timeout_block == NULLtimeout check is completely bypassed).Py_CheckTimeOutremodule integrationBy tapping into the existing signal‑check counter inside _sre, timeout checks add no measurable overhead to normal regexps while allowing catastrophic backtracking to be safely interrupted within the specified deadline.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
https://discuss.python.org/t/a-lightweight-cooperative-timeout-mechanism-for-synchronous-code-with-timeout-seconds/107811
Linked PRs