Make cron-like jobs run regularly in Python

There is a library called huey that implements task queues in python. Compared to celery, which is famous for the library of the same task queue, you can implement periodic job execution more simply. This time, I will not touch on normal task execution.

Advance preparation

--Installing the library

pip install huey
pip install redis

--Starting Redis for broker

Easy to start with docker

docker run --rm -d -p 6379:6379 redis

Describe the task

This time we started Redis on the broker, so we call Redis Huey. You can register a periodic job just by adding a periodic_task decorator to the function.

main.py


from huey import RedisHuey, crontab
import time

huey = RedisHuey(host="127.0.0.1")


@huey.periodic_task(crontab(minute="*"))
def test():
    print("process start")
    time.sleep(5)
    print("process end")

Specify the job execution time with the crontab given as the decorator argument. This time is every minute.

Start Worker

After installing huey, you can use the command huey_consumer, which will start the worker process.

$ huey_consumer main.huey

[2020-07-16 23:18:59,827] INFO:huey.consumer:MainThread:Huey consumer started with 1 thread, PID 2744 at 2020-07-16 14:18:59.827805
[2020-07-16 23:18:59,828] INFO:huey.consumer:MainThread:Scheduler runs every 1 second(s).
[2020-07-16 23:18:59,828] INFO:huey.consumer:MainThread:Periodic tasks are enabled.
[2020-07-16 23:18:59,828] INFO:huey.consumer:MainThread:The following commands are available:
+ main.test
[2020-07-16 23:18:59,836] INFO:huey.consumer.Scheduler:Scheduler:Enqueueing periodic task main.test: 30a61be7-903c-4fa7-815f-c5c013074085.
[2020-07-16 23:18:59,841] INFO:huey:Worker-1:Executing main.test: 30a61be7-903c-4fa7-815f-c5c013074085
process start
process end
[2020-07-16 23:19:04,847] INFO:huey:Worker-1:main.test: 30a61be7-903c-4fa7-815f-c5c013074085 executed in 5.005s
[2020-07-16 23:19:59,830] INFO:huey.consumer.Scheduler:Scheduler:Enqueueing periodic task main.test: 8166b273-1b97-427a-a349-a2772ea67fd2.
[2020-07-16 23:19:59,834] INFO:huey:Worker-1:Executing main.test: 8166b273-1b97-427a-a349-a2772ea67fd2
process start
process end
[2020-07-16 23:20:04,839] INFO:huey:Worker-1:main.test: 8166b273-1b97-427a-a349-a2772ea67fd2 executed in 5.005s

You can see that the job is running every minute.

that's all

Recommended Posts

Make cron-like jobs run regularly in Python
Run automatic jobs in python
Run shell commands in python
Run Python unittests in parallel
Don't make test.py in Python!
Make a bookmarklet in Python
Make Opencv available in Python
Make python segfault in 2 lines
Make python segfault in one line
Let's run "python -m antigravity" in python
Run shell command / python in R
Make standard output non-blocking in Python
Make python segfault in three lines
Run unittests in Python (for beginners)
Run a simple algorithm in Python
Make ASCII art GIF animation in Python
Run AzureKinect in Python on Christmas Eve.
Run the Python interpreter in a script
Automate jobs by manipulating files in Python
Let's make a combination calculation in Python
Run Python in C ++ on Visual Studio 2017
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Run Google Analytics API (core v3) in python
Run Python YOLOv3 in C ++ on Visual Studio 2017
Epoch in Python
Discord in Python
Make a copy of the list in Python
Sudoku in Python
Until you run the changefinder sample in python
DCI in Python
quicksort in python
nCr in python
[Mac] Run the RealSense D415 sample in Python
Plink in Python
Constant in python
Let's make some notification processing samples in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
Make a joyplot-like plot of R in python
StepAIC in Python
How to run Leap Motion in non-Apple Python
N-gram in python
LINE-Bot [0] in Python
Disassemble in Python
Reflection in Python
Constant in python
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python