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.

Source Distribution

pyrunnable-0.42.tar.gz (2.2 kB view hashes)

Uploaded Source

Built Distributions

pyrunnable-0.42-py3.8.egg (3.2 kB view hashes)

Uploaded Source

pyrunnable-0.42-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

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