Python Module to enable running tasks inside threads based on cron schedule
Project description
Cron Threads
A Python module whose goal is to simplify working with threads which need to trigger it`s internal tasks via a cron schedule.
Working Modes
- Command : used to run os specific commands in a shell in a separate thread
- File : import a specific python script and run a function within it as a separate thread. Very good for running scripts based on template
- Internal : run just an internal function in a separate thread
Schedules
- If a schedule is provided in cron format, it will execute whatever is specified based on that specific schedule
- If no schedule is provided or is left empty, then it will work as oneshot, were after the work is done, the thread will be deleted.
Workflow logic
- Rather than start a thread directly which you have to manage manually, a thread manager has been created which holds references to each thread it was created by it.
- The Manager has a function which enables management of threads, specifically monitoring for when threads exit or for when the tasks inside threads finish executing and relay-ing that info to the main thread.
- The thread will always stay online if possible if a cron schedule is provided, an internal timer will just suspend the thread until it is time for whatever needes to be run to be executed again.
Thread Output
- Ideally the task given to the thread should always return 3 things : exit code(int), output(str) and error(str), in that specific order
- If the task has no return statements, or it returns less that the 3 mentioned items, it will suppliment with None returns
- If more than 3 items are returned, it will throw an error mentioning that only 3 items can be returned.
Usage/Examples
from cron_threads import ThreadManager, WorkerModes
def internal(group):
"""example function to just print whatever is given as argument"""
print(group)
manager = ThreadManager()
manager.begin_manage()
# Example usage
manager.start_thread("thread1", "* * * * *",WorkerModes.COMMAND,"dir") # run the dir command as a separate shell
manager.start_thread("thread2", "* * * * *",WorkerModes.FILE,"file_name",func_name="run") # run a separate function from within another python script
manager.start_thread("thread3", None, WorkerModes.INTERNAL, internal, None, "123") # use an internal function as the task of the thread
manager.stop_thread("thread1") # will send a signal to the tread to close. If the task of the thread is running, it will wait for the task to finish, then close the thread
# simple while true logic to monitor what is going on with the threads
while True:
while manager.have_threads_exited() > 0:
print(manager.get_thread_exit_info())
while manager.have_tasks_exited() > 0:
print(manager.get_task_exit_info())
if some_test_here_is_true:
manager.stop_manage() # this will kill all threads as well
Future improvements
- Force kill thread even when task is running
- Resource locking
- Community Requests which make sense
- TBD
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cron_threads-0.1.4.tar.gz
(17.3 kB
view details)
Built Distribution
File details
Details for the file cron_threads-0.1.4.tar.gz
.
File metadata
- Download URL: cron_threads-0.1.4.tar.gz
- Upload date:
- Size: 17.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e84c966534cc75d182d8b89d574466887ed2cb9952ae1684cd025b18ccc80ac6 |
|
MD5 | 7a28b4a43252f3cd1cf1fb18c13504c5 |
|
BLAKE2b-256 | 8d91b0d7cadd3e4bfcf2667c378fc659efb02a3705759e2970384d98880dbc3a |
File details
Details for the file cron_threads-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: cron_threads-0.1.4-py3-none-any.whl
- Upload date:
- Size: 17.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.19
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d24d592a00dbf4fbe47b0096a177418af1a689c80b73aa5dca90b6ea500f2af |
|
MD5 | f686880e32b0304739b3e5971d8473e4 |
|
BLAKE2b-256 | 9bb328bb9aa2236258c665b1d5fd7843bc47de7667bf9e075d5053356b7a82ed |