Skip to main content

A package providing some simple extensions of the Thread class for Python.

Project description

jthreads

jthreads is a Python library that extends on the Python Thread class by adding extra functionality such as LoopingThread, Timer, Interval, CountdownTimer and CallbackCountdownTimer.

Installation

Use the package manager pip to install jthreads.

pip install jthreads

Usage

LoopingThread

from jstreams import LoopingThread
from time import sleep

class MyThread(LoopingThread):
    def __init__(self) -> None:
        LoopingThread.__init__(self)
        self.count = 0
    
    def loop(self) -> None:
        # Write here the code that you want executed in a loop
        self.count += 1
        print(f"Executed {self.count} times")
        # This thread calls the loop implementation with no delay. Any sleeps need to be handled in the loop method
        sleep(1)
thread = MyThread()
thread.start()
sleep(5)
# Stop the thread from loopiong
thread.cancel()

CallbackLoopingThread

This looping thread doesn't require overriding the loop method. Instead, you provide a callback

from jthreads import CallbackLoopingThread
from time import sleep

def threadCallback() -> None:
    print("Callback executed")
    sleep(1)

thread = CallbackLoopingThread(threadCallback)
# will print "Callback executed" until the thread is cancelled
thread.start()

sleep(5)
# Stops the thread from looping
thread.cancel()

Timer

The Timer thread will start counting down to the given time period, and execute the provided callback once the time period has ellapsed. The timer can be cancelled before the period expires.

from jthreads import Timer
from time import sleep

timer = Timer(10, 1, lambda: print("Executed"))
timer.start()
# After 10 seconds "Executed" will be printed
from jthreads import Timer
from time import sleep

# The first parameter is the time period, the second is the cancelPollingInterval.
# The cancel polling interval is used by the timer to check if cancel was called on the timer.
timer = Timer(10, 1, lambda: print("Executed"))
timer.start()
sleep(5)
timer.cancel()
# Nothing will be printed, as this timer has been canceled before the period could ellapse

Interval

The interval executes a given callback at fixed intervals of time.

from jthreads import Interval
from time import sleep
interval = Interval(2, lambda: print("Interval executed"))
interval.start()
# Will print "Interval executed" every 2 seconds
sleep(10)
# Stops the interval from executing
interval.cancel()

CountdownTimer

The countdown timer is similar in functionality with the Timer class, with the exception that this timer cannot be canceled. Once started, the callback will always execute after the period has ellapsed.

from jthreads import CountdownTimer

CountdownTimer(5, lambda: print("Countdown executed")).start()
# Will always print "Countdown executed" after 5 seconds

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

jthreads-1.0.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

jthreads-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file jthreads-1.0.0.tar.gz.

File metadata

  • Download URL: jthreads-1.0.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for jthreads-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f7676388d6d2a10126d22fd214b9ecc7102f553e6ec77618673ac2c08ac2f179
MD5 8cb9b61792f6cefbf50c1b5e85b160cb
BLAKE2b-256 af5c52838fa7f7987ec050eff82c590ec1a001b45ee7d513f51c6de49509c278

See more details on using hashes here.

File details

Details for the file jthreads-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: jthreads-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for jthreads-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5eb01ca3bbcd16970de19460f2fd6c2f23b6faaa68158cbe8b6510999e0f0f39
MD5 8fb14e45f2e5099ebf53a1b55d8536da
BLAKE2b-256 427a7275f99331c2bc73d8e3e41c3a049c3f22413e31d5775304f9177a9d7db4

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