When I was looking for some good ideas for running cron jobs in Python, I found a library called ** APScheduler **.
Basically, you can do the following.
This time, I was planning to hit the API every 10 minutes and create a person who would save the data if the information could be obtained, so this requirement was fully satisfied. After that, it is quite convenient because there are methods to start Scheduler for each environment and framework.
$ pip install apscheduler
app/schedule.py
def hello_world():
print("Hello World!")
sched = BackgroundScheduler(standalone=True,coalesce=True)
sched.add_job(hello_world, 'interval', minutes=1)
sched.start()
This will display "Hello World!" Every minute.
I made an application with Flask, so if I read it with __init__.py
, it worked fine in both the local environment and the production environment (Heroku).
app/__init__.py
import app.schedule
If I read the library and proceed, nothing will happen, but unexpectedly Japanese articles did not come out immediately, so I left it as a memorandum.
Recommended Posts