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.

Files for kthread, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size kthread-0.2.2-py2-none-any.whl (3.9 kB) File type Wheel Python version py2 Upload date Hashes View hashes
Filename, size kthread-0.2.2.tar.gz (3.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page