Skip to main content

Stoppable thread with common stop signalling

Project description

Attentive provides an attentive thread

Documentation Status https://github.com/sthysel/attentive/blob/master/docs/attentive.jpg?raw=true
from attentive import StoppableThread, set_signal_handler
from random import randint


class Man(StoppableThread):
    def __init__(self, name):
        StoppableThread.__init__(self)
        self.name = name

    def run(self):
        print('{} has quickened'.format(self.name))
        while not self.stopped:
            self.sleep(randint(1, 10))
            print('{} throws a {}'.format(self.name, randint(1, 6)))

        print('{} expires'.format(self.name))


stopper = set_signal_handler()
with Man('Trump'), Man('Wang'), Man('Erdoğan'):
    while not stopper.is_set():
        stopper.wait(1)

Example Run

🚼Trump has quickened
🚼Wang has quickened
🚼Erdoğan has quickened
Wang throws a 
Trump throws a 
Erdoğan throws a 
Wang throws a 
Wang throws a 
Erdoğan throws a 🍭
Erdoğan throws a 
Trump throws a 
^CErdoğan throws a 🍼
  Erdoğan expires
Wang throws a 
  Wang expires
Trump throws a 🍼
  Trump expires

Use attentive if you need to wire up a some worker threads that needs to cleanly shut themselves down on a SIG_INT or SIG_TERM.

StoppableThread is a context managed thread that lives on while in context. Once it exists context it sets its internal stopped flag that are periodically checked for state. This signals thread state allowing the thread to cleanly exit.

External state is controlled by a signal event, exiting the main context loop.

Internally use the StoppableThread.sleep() method that is interrupted when stop()ed during sleep.

Install

Install from pypi

$ pip install attentive

Install from source

$ pip install .

Versioning

Current version is 0.1.3

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

attentive-0.1.3-py2.py3-none-any.whl (5.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file attentive-0.1.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for attentive-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 64285ed056f6a23ddf59ecf3f00632be5c1773fe1f8d5566c5dacbbeccdc50d5
MD5 2e0a51c56082587407d4af143e8920bb
BLAKE2b-256 a26119f443622e74301ed45d820d00852617ab3313d4b57feb1a7b799580c953

See more details on using hashes here.

Supported by

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