Skip to main content

Killable threads in Python!

Project description

Build Status

kthread

Killable threads in Python!

Purpose

The built-in threading.Thread class offers no simple solution to terminate a running thread. kthread.KThread inherits threading.Thread and supplies methods named exit(), kill(), and terminate() that serve the same purpose: attempt to stop a thread if it's running.

How it works

KThread leverages the CPython API to raise a SystemExit exception on a given thread. Assuming that the thread is not blocked by an operating system call (such as sleep, accept, or recv), the thread will forcefully quit.

DISCLAIMER

TERMINATING THREADS MAY INTRODUCE INSTABILITY OR OTHER UNDESIRABLE EFFECTS IN YOUR PROGRAMS. THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY. THE MUNSHI GROUP CANNOT BE HELD LIABLE FOR ANY DAMAGES, LOSSES, OR EXPENSES INCURRED BY YOU OR YOUR ORGANIZATION WHILE USING THIS SOFTWARE.

Installation

To install this package, run the following command:

$ pip install kthread

Usage

>>> import time
>>> import kthread
>>> import sys
>>> def func():
>>>     try:
>>>         while True:
>>>             time.sleep(0.2)
>>>     finally:
>>>         sys.stdout.write("Greetings from Vice City!\n")
>>>         sys.stdout.flush()
>>>
>>> t = kthread.KThread(target = func, name = "KillableThread1")
>>> t.start()
>>> t.isAlive()
True
>>> t.terminate()
Greetings from Vice City!
>>> t.isAlive()
False

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kthread-0.2.3.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

kthread-0.2.3-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file kthread-0.2.3.tar.gz.

File metadata

  • Download URL: kthread-0.2.3.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for kthread-0.2.3.tar.gz
Algorithm Hash digest
SHA256 90e194e6a7ff903040c4133d3ea9037c908c4296bf5f582c7fdcf6325a04f9b4
MD5 d91b58af95ac583c0f7fbe63e1d53ce0
BLAKE2b-256 189baa1b48c3cf6e1a914ee5eee1fed77cd7217fb0a35c07c345da4ec5215cae

See more details on using hashes here.

File details

Details for the file kthread-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: kthread-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.7

File hashes

Hashes for kthread-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 808d3bb0ec6d573c8a00c10dfabe81b7e87d4ac945cb58335432c17f8db78ca6
MD5 d0e2a9c818b01fcc5811a4c8920e3196
BLAKE2b-256 82e34c26efac0a7e4e053eb0042164f3ea7cdee8dfc7eecda521e6d00ce45ea1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page