skillfed

kthread

Killable threads in Python!

kthread v0.2.3 90.0K downloads/30d#13,626 on PyPI16
Permissive license Abandoned released

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 kthread

uv

uv add kthread

poetry

poetry add kthread

Installing 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

License :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 2.7Programming Language :: Python :: 3

Tags

kill thread pythonterminate thread forcefullystop running threadthread terminationkillable threadsthread exitforce thread stop
threadinglegacy-maintenance-risk

More Software Development packages