Set a simple periodic execution of a function.
In development
- Set a periodic task with or without parameter(s).
- Start a runner to call a task to a specified interval.
- Stop the runner as well as restart it.
- Change parameter(s) during running.
- No external dependencies.
- Tested on Python 3.5 and 3.6.
pip install tinyPeriodicTask
If you want all, please read https://help.github.com/articles/cloning-a-repository/
- interval: time in second between executions.
- callback: callable function to run once the interval is reach.
The interval time is running in a deamon thread. This to ensure the time has no interference to the main execution, and vice versa.
By design, when you start a tinyPeriodicTask instance, the runner will delay the first call to the callback function according to the interval.
When you create an instance of TinyPeriodicTask, you can add any parameters you need to use when executing the callback. like this:
from tinyPeriodicTask import TinyPeriodicTask
#The function to periodically run
def task(message='this'):
print("I'm working on {0}".format(message))
task = TinyPeriodicTask(3, task)
task.start()
task2 = TinyPeriodicTask(3, task, message='that')
task2.start()
try:
print('Execution en cours')
#Keep the main process alive
# otherwise the task will be executed only one time
while True:
time.sleep(0.5)
except KeyboardInterrupt:
task1.stop()
task2.stop()
print('Loop stopped')
Result:
$ python exemple.py
I'm working on that
Execution en cours
I'm working on this
I'm working on this
I'm working on that
I'm working on this
I'm working on this
I'm working on that
I'm working on this
I'm working on this
I'm working on that
I'm working on this
Loop stopped
Set a periodic execution of a task.
class TinyPeriodicTask(interval, callback, *args, **kwargs)
- interval (number) time in second between execution. 0 or negatif number is changed to 1.
- callback (function) callable function to call once the interval is reach.
- *args, **kwargs parameter(s) to use when executing the callback function.
- Exception If callback is not a callable function
Create and transmit a setting instance.
Set an interval specifying its unit (second, minute, hour, day)
from tinyPeriodicTask.IntervalSettings import IntervalSettings
from tinyPeriodicTask.IntervalUnitEnum import IntervalUnit
from tinyPeriodicTask import TinyPeriodicTask
settings = IntervalSettings(5, IntervalUnit.hour)
task = TinyPeriodicTask(settings, yourTaskFunction)
Set a job every day
from tinyPeriodicTask.StartAtSettings import StartAtSettings
from tinyPeriodicTask import TinyPeriodicTask
settings = StartAtSettings("15:30")
task = TinyPeriodicTask(settings, yourTaskFunction)
Start the periodic runner
If the runner is stopped, it will restart. If it is already started, it will do nothing.
tinyPeriodicTask = TinyPeriodicTask(5, anyCallback)
tinyPeriodicTask.start()
Stop or pause the periodic runner.
tinyPeriodicTask = TinyPeriodicTask(5, anyCallback)
tinyPeriodicTask.start()
...
tinyPeriodicTask.stop()
You can change the interval value during the runner execution. This is usefull when you want to change the rhythm over time: let say every 5 seconds during the day to 3600 (1hour) during the night. however, TinyPeriodicTask does not take those conditions (day and night) yet. So you have to manage them in your code.
tinyPeriodicTask.interval = 3600 #1 hour
Let you change the parameter value.
This will restart the runner once the parameters changed.
tinyPeriodicTask.useThis(*args, **kwargs)
- *args, **kwargs parameter(s) to use when executing the callback function.
- Exception If callback is not a callable function
Distributed under the MIT license: https://opensource.org/licenses/MIT
Copyright (c) 2017 Frédérick Lussier (www.linkedin.com/in/frederick-lussier-757b849)