Skip to main content

A pure-python periodic timer that can be started multiple times

Project description

multitimer

A pure-python auto-repeating timer that can be stopped and restarted multiple times.

multitimer.MultiTimer is similar to threading.Timer, but allows the timer to repeat multiple times. Additionally, MultiTimer can be started and stopped multiple times (unlike threading.Timer).

Overview

multitimer.MultiTimer(interval, function, args=None, kwargs=None, count=-1, runonstart=True)

Creates a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed, a total of count times.

If runonstart==True, then function will be called immediately when .start() is called.

If args is None (the default) then an empty list will be used. If kwargs is None (the default) then an empty dict will be used.

If count == -1 (the default), the timer will repeat indefinitely, or until .stop() is called.

Start this timer by calling .start(). Once started, calling .stop() will terminate the timer's loop and not produce any further calls to function. Note that if function is currently in the middle of running, it will finish the current iteration and not be interrupted.

ontimeout and params were deprecated in 0.2 and replaced by function, args and kwargs to match the threading.Timer API. ontimeout and params have been removed in 0.3.

Since the underlying mechanism is purely based on python threads & events, the overall processor load & memory usage are minimal. Note that the timing accuracy is typically to within about 10 ms, depending on the platform.

Installation & usage

$ pip install multitimer
import multitimer
import time

def job():
	print("I'm working...")

# This timer will run job() five times, one second apart
timer = multitimer.MultiTimer(interval=1, function=job, count=5)

# Pauses for one interval before starting job() five times
timer = multitimer.MultiTimer(interval=1, function=job, count=5, runonstart=False)


# You can specify input parameters for the _function_ function
def job2(foo):
	print(foo)

timer = multitimer.MultiTimer(interval=1, function=job2, kwargs={'foo':"I'm still working..."})

# Also, this timer would run indefinitely...
timer.start()

# ...unless it gets stopped
time.sleep(5)
timer.stop()

# and potentially waited for (in case an iteration was in progress)
timer.join()


# If a mutable object is used to specify input parameters, it can be changed after starting the timer
output = {'foo':"Doin' my job again."}
timer = multitimer.MultiTimer(interval=1, function=job2, kwargs=output, count=5)
timer.start()

time.sleep(3.5)
output['foo'] = "I'd like to be done now."

# Note: While this feature can be useful, be aware that changing arguments while the timer is running may result in some
# race conditions. multitimer is multithreaded but does not currently have any sort of locking mechanisms in place to
# ensure that operations are atomic. 


# And a MultiTimer can be re-started by just calling start() again
time.sleep(2)
output['foo'] = 'Please just let me be...'
timer.start()
time.sleep(4.5)
timer.stop()

Releases

0.3, 2020-11-27

  • Add a .join() method to wait for a timer that has been stopped to complete its final iteration. (Thanks, @pakal!)
  • Remove ontimeout and params arguments (deprecated in 0.2)
  • Properly pass args to RepeatingTimer
  • Fix error if .stop() called before .start()

0.2, 2019-01-17

  • Replace time.clock() calls with time.perf_counter(), as time.clock is deprecated since python 3.3 and doesn't provide consistent behavior across different platforms.
  • Replace ontimeout with function, and params with args and kwargs, to match the threading.Timer API. ontimeout and params are deprecated and will be removed in v0.3.
  • Add lots of code comments to better explain how the module works.

0.1, 2018-02-15

  • Initial release

Meta

Josh Burnett - josh_github@burnettsonline.org

Distributed under the MIT license. See LICENSE.txt for more information.

https://github.com/joshburnett/multitimer

Hope you find this useful!

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

multitimer-0.3.zip (12.7 kB view details)

Uploaded Source

Built Distribution

multitimer-0.3-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file multitimer-0.3.zip.

File metadata

  • Download URL: multitimer-0.3.zip
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4

File hashes

Hashes for multitimer-0.3.zip
Algorithm Hash digest
SHA256 e06659150538e62c5ccb531b67a798e3b034ad2bb5ca024ebdef52fa66759274
MD5 b8ba7c49a45c8fce85f98296af01a266
BLAKE2b-256 ed2a0094d6a6cdf9f72de1c853d10d668d9148ed3ea2280fdcaf2755882b1dee

See more details on using hashes here.

File details

Details for the file multitimer-0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: multitimer-0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.23.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.4

File hashes

Hashes for multitimer-0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6becf821cb0ce111af40bbd1c6ee58201b9594ff3bb97513c991c7ff0f658549
MD5 8c6bc0738617baf011adb6ad3b43e961
BLAKE2b-256 19efd0b52adee5bb0c3f29091b341dd0bd1374d18f191b1c583f4bc199d4d703

See more details on using hashes here.

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