Skip to main content

threading.Thread wrapper with convenient functions

Project description

why?

i just can’t be bothered to write the same boilerplate code over and over again. so i wrote it into a little library. it has the ‘android’ lifecycle in mind:

  • on_start (before the loop gets called)
  • work (in the loop)
  • on_stop (when the loop has finished)

also it has a useful function called ‘stop’

how to…

… install

pip3 install git+https://github.com/smthnspcl/pyrunnable

… use it

from runnable import Runnable
from time import sleep

class ThreadedObject(Runnable):
    def __init__(self, **kwargs):  # not necessary but included for the sake of completeness
        Runnable.__init__(self, **kwargs)  # gotta super to Runnable as you would with Thread

    def on_start(self):
        print("we are going to run in a loop")

    def work(self):
        print("working")
        sleep(0.42)  # be nice to the cpu

    def on_stop(self):
        print("we finished working. time for beer")

o = ThreadedObject()
try:
    o.start()
    o.join()  # Runnable inherits threading.Thread
except KeyboardInterrupt:
    o.stop()

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 pyrunnable, version 0.42
Filename, size File type Python version Upload date Hashes
Filename, size pyrunnable-0.42.tar.gz (2.2 kB) File type Source Python version None Upload date Hashes View
Filename, size pyrunnable-0.42-py3.8.egg (3.2 kB) File type Egg Python version 3.8 Upload date Hashes View
Filename, size pyrunnable-0.42-py3-none-any.whl (3.8 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page