PyTimeoutAfter
A signal free, multi-threading safe, method of setting A Python mid-function Timeout
Install / Use
/learn @dr-luke/PyTimeoutAfterREADME
Timeout After (X Seconds)
A signal free, multi-threading safe, method of setting A mid-function Timeout
In an effort to find a non-blocking, minimally intrusive, replacement for the signal method of making a mid-function timeout, I stumbled upon this "Nasty hack" posted by liuw on GitHub, which allowed me to actual do what I originally though was impossible.
As LIU points out, this is a NASTY hack and is only possible by breaking the C level interpreter to allow this to happen. This can be a point of concern and my be fixed in the future, but as for now it seems safe to use with the current version of python 3 (Tested on 3.6.5 - 3.8.0)
And if I haven’t stressed this enough, I (we) take no responsibility for the use of this exploit, use at your own risk.
References
- https://gist.github.com/liuw/2407154 (Nasty Hack)
- https://www.jujens.eu/posts/en/2018/Jun/02/python-timeout-function/ (Code Style Inspiration)
Example Usage
import timeout_after
def test_me():
try:
with TimeoutAfter(timeout=3, exception=TimeoutError):
for x in range(0, 6):
print('Tick {}'.format(x + 1))
time.sleep(1)
print("I don't say anything...")
except TimeoutError:
print('Looks like we ran out of ticks...')
thread = threading.Thread(target = test_me)
thread.start()
thread.join()
Related Skills
node-connect
349.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
claude-opus-4-5-migration
109.5kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
frontend-design
109.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
model-usage
349.2kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
