Another library you can use is django-q
Django Q is a native Django task queue, scheduler and worker application using Python multiprocessing. 1
Like django-appscheduler
it can run and track jobs using the database Django is attached to. Or, it can use full-blown brokers like Reddis.
The only problem is I need my python program that gets the tweets to be run in the background every-so-often.
That sounds like a scheduler. (Django-q also has a tasks feature, that can be triggered by events rather than being run on a schedule. The scheduler just sits on top of the task feature, and triggers tasks at a defined schedule.)
There's three parts to this with django-q:
- Install Django-q and configure it;
- Define a task function (or set of functions) that you want to fetch the tweets;
- Define a schedule that runs the tasks;
- Run the django-q cluster that'll process the schedule and tasks.
Install django-q
pip install django-q
Configure it as an installed app in Django settings.py
(add it to the install apps list):
INSTALLED_APPS = [
...
'django_q',
...
]
Then it needs it's own configuration settings.py
(this is a configuration to use the database as the broker rather than reddis or something external to Django.)
# Settings for Django-Q
# https://mattsegal.dev/simple-scheduled-tasks.html
Q_CLUSTER = {
'orm': 'default', # should use django's ORM and database as a broker.
'workers': 4,
'timeout': 30,
'retry': 60,
'queue_limit': 50,
'bulk': 10,
}
You'll then need to run migrations on the database to create the tables django-q uses:
python manage.py migrate
(This will create a bunch of schedule and task related tables in the database. They can be viewed and manipulated through the Django admin panel.)
Define a task function
Then create a new file for the tasks you want to run:
# app/tasks.py
def fetch_tweets():
pass # do whatever logic you want here
Define a task schedule
We need to add into the database the schedule to run the tasks.
python manage.py shell
from django_q.models import Schedule
Schedule.objects.create(
func='app.tasks.fetch_tweets', # module and func to run
minutes=5, # run every 5 minutes
repeats=-1 # keep repeating, repeat forever
)
You don't have to do this through the shell. You can do this in a module of python code, etc. But you probably only need to create the schedule once.
Run the cluster
Once that's all done, you need to run the cluster that will process the schedule. Otherwise, without running the cluster, the schedule and tasks will never be processed. The call to qcluster is a blocking call. So normally you want to run it in a separate window or process from the Django server process.
python manage.py qcluster
When it runs you'll see output like:
09:33:00 [Q] INFO Q Cluster fruit-november-wisconsin-hawaii starting.
09:33:00 [Q] INFO Process-1:1 ready for work at 11
09:33:00 [Q] INFO Process-1:2 ready for work at 12
09:33:00 [Q] INFO Process-1:3 ready for work at 13
09:33:00 [Q] INFO Process-1:4 ready for work at 14
09:33:00 [Q] INFO Process-1:5 monitoring at 15
09:33:00 [Q] INFO Process-1 guarding cluster fruit-november-wisconsin-hawaii
09:33:00 [Q] INFO Q Cluster fruit-november-wisconsin-hawaii running.
There's also some example documentation that's pretty useful if you want to see how to hook up tasks to reports or emails or signals etc.