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.1.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.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: jthreads-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 4f84ccd86b78b2c7b44b4ca9f07727185ad9b8a503de419b359a363836a739ed
MD5 ebf9806e58c1826d70ba2eb1d2ee161d
BLAKE2b-256 f3c7f4c86907bad4e22407c35bd9b161ffa13710ff86d4dc29d11f0d49d83699

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jthreads-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ac75fbb5929aab9377c13aaa5ff959b6a66e6d78cc42b9dd02e4689bff850127
MD5 7d2edb13b5402148cfcd032d86e44c85
BLAKE2b-256 a682c3614ab5ad9c6dc63785fad87c4c7c099ea7367ef98f8a17068696dc09be

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