Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
python: end a pool worker on SIGTERM even when it waits on a lock
Mohit Mishra69f97b9e9a69
diff --git a/src/python/sitecustomize.py b/src/python/sitecustomize.pyindex 92c096c..4661112 100644--- a/src/python/sitecustomize.py+++ b/src/python/sitecustomize.py@@ -31,12 +31,18 @@ def _fast_anything_watch_workers():def _on_term(*_):# a second SIGTERM must not cut the worker off while it writes its resultssignal.signal(signal.SIGTERM, signal.SIG_IGN)- if state is not None:- state.finish()- signal.signal(signal.SIGTERM, signal.SIG_DFL)- os.kill(os.getpid(), signal.SIGTERM)+ try:+ if state is not None:+ state.finish()+ finally:+ signal.signal(signal.SIGTERM, signal.SIG_DFL)+ os.kill(os.getpid(), signal.SIGTERM)signal.signal(signal.SIGTERM, _on_term)+ try:+ _fast_anything_nudge_on_term(signal)+ except Exception as e: # never break the app's workers+ sys.stderr.write("fast-anything: can't watch for SIGTERM: %r\n" % (e,))try:state = sys.modules.get("_fast_anything_state")if state is None: # a fresh interpreter (spawn, forkserver): load the runner@@ -62,6 +68,30 @@ def _fast_anything_watch_workers():_fast_anything_graceful_pool_terminate()+def _fast_anything_nudge_on_term(signal):+ """A Python signal handler runs only when the main thread gets back to Python code. If SIGTERM+ lands just before a blocking call starts (a worker waiting on the pool's queue lock, which+ terminate() holds for good), the call never returns and the handler never runs. So the signal+ also wakes a thread, which keeps sending SIGTERM to the main thread until the handler ends it."""+ import threading+ import time++ r, w = os.pipe()+ os.set_blocking(w, False)+ signal.set_wakeup_fd(w, warn_on_full_buffer=False) # an app's own wakeup fd replaces this one+ main = threading.get_ident()++ def nudge():+ signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGTERM}) # let the main thread take it+ while signal.SIGTERM not in os.read(r, 64):+ pass+ while True:+ time.sleep(0.1)+ signal.pthread_kill(main, signal.SIGTERM)++ threading.Thread(target=nudge, name="fast-anything-sigterm", daemon=True).start()++def _fast_anything_graceful_pool_terminate():"""On Windows, Pool.terminate() (and `with Pool(...) as p:`) uses TerminateProcess(), whichkills workers before BaseProcess._bootstrap's finally block can write their profile/recordings.