kthread
Killable threads in Python!
What it is and what it does
KThread is a thin wrapper around Python's threading.Thread that adds methods to forcefully terminate a running thread by injecting a SystemExit exception via the CPython API. It solves the problem that the standard library offers no built-in way to stop a thread once started—you can only wait for it to finish naturally.
The package works by leveraging low-level CPython internals to raise an exception inside a target thread. Threads blocked on OS calls (like time.sleep, socket.accept, or socket.recv) will not respond to termination. The author includes a strong disclaimer that thread termination can introduce instability, resource leaks, or other undesirable effects, and provides no warranty. It is intended for cases where you have no other way to stop a thread and accept the risks.
Use it for:
- Stopping long-running worker threads when a parent process receives a shutdown signal
- Cleaning up threads that enter infinite loops and cannot be interrupted by normal means
- Testing code that spawns threads, where you need to forcibly halt them between test cases
- Implementing timeout mechanisms for thread-based operations when standard timeout patterns are unavailable
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
KThread provides a way to forcefully terminate running threads by raising a SystemExit exception within them, extending Python's standard threading.Thread class with kill(), terminate(), and exit() methods.
No. The package is abandoned (last update 2022-02-19) and comes with explicit warnings that thread termination can cause instability. Modern Python code should use threading.Event, queue.Queue, or async/await for controlled thread shutdown. Install only if you have a specific legacy codebase already using it and cannot refactor to safer patterns.
Install
kthread on PyPI
pip
pip install kthreaduv
uv add kthreadpoetry
poetry add kthreadInstalling kthread
Before you install
Installation is straightforward with no runtime dependencies. However, the package is abandoned—last release was 2022-02-19 with no updates since, and the repository shows minimal activity (16 stars). Use only if you accept the maintenance risk.
License in practice
MIT license permits free use, modification, and distribution with minimal restrictions, making it legally safe to integrate into most projects.
Quickstart
import time
import kthread
def worker():
while True:
time.sleep(0.2)
t = kthread.KThread(target=worker)
t.start()
t.terminate() # or t.kill() or t.exit()
Threads blocked on OS calls (sleep, socket operations) may not respond to termination. The package uses CPython internals and may not work on non-CPython implementations.
Verify before relying
- Whether the CPython API approach works reliably across Python 3.x versions beyond what the classifiers claim
- Specific Python versions where OS-blocked threads (sleep, accept, recv) cannot be interrupted
- Compatibility with async/await patterns or modern concurrency libraries
Package facts
| License | not declared (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | abandoned — 1,637 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 89,985/month — #13,626 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: kthread-0.2.3-py3-none-any.whl
Keywords: threading, threads, terminate
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
func-timeoutRuns any Python function with a specified…
copyleft · top 5,000 on PyPI
threadedProvides decorators to wrap functions for…
permissive · top 15,000 on PyPI
stopitProvides context managers and decorators to…
permissive · top 15,000 on PyPI
pytest-reraiseCaptures exceptions raised in threads during…
permissive · top 15,000 on PyPI
Flask-ThreadsProvides thread and thread pool helpers that…
unclear · top 5,000 on PyPI
simpervisorProvides async methods to start, manage, and…
permissive · top 15,000 on PyPI
lib-cli-exit-toolsProvides portable signal handling,…
permissive · top 15,000 on PyPI
pytest-threadleakA pytest plugin that detects when tests leak…
permissive · top 15,000 on PyPI
opentelemetry-instrumentation-threadingEnsures OpenTelemetry context is propagated…
permissive · top 1,000 on PyPI
pytest-run-parallelA pytest plugin that runs test functions…
permissive · top 15,000 on PyPI