18

I am new to Django and the django-background-tasks package.

I am facing an issue that I couldn't do/start background task unless I forcefully run the command process_tasks , that is python manage.py process_tasks. I want to do/start background task without run the process_tasks command.

settings.py

MAX_ATTEMPTS=1
BACKGROUND_TASK_RUN_ASYNC = True

tasks.py

from background_task import background
#included necessary packages for SMTP
@background(schedule=5)
def test():
 #send mail to some ids

views.py

def index(request):
 test(schedule=5)
 return HttpResponse("Hello, world. ")

Ignore my logic.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Dec 18, 2016 at 4:59

7 Answers 7

8

In case anybody else is trying to get this working - I call a shell script from my Dockerfile that then runs two commands. One to start the webserver (gunicorn or run_server) and one to run the manage.py process_tasks process. The important thing is to run the process_tasks process as a background task.

At the bottom of my Dockerfile I have: CMD ["./run_app.sh"]

And my run_app.sh file looks like:

#!/usr/bin/env bash
# start background tasks 
python manage.py process_tasks &
gunicorn --workers=3 app.wsgi -b 0.0.0.0:8080

Note the trailing & from the process tasks command. This enables the shell script to continue processing and run the gunicorn command.

Hope this helps someone.

answered Nov 14, 2019 at 16:21
Sign up to request clarification or add additional context in comments.

Comments

1

Well you could make a bash script that makes the process_tasks call and call that script from python

to call the script from python you can use

import subprocess
subprocess.call("process_tasks.sh", shell=True)

as its explained here Running bash script from within python

your bash script may look something like this

cd your/virtualenv/directory
source virtualenv/bin/activate
exec /path/to/manage.py process_tasks

dont forget to make process_tasks.sh executable

answered Oct 5, 2018 at 10:41

Comments

1

You must run process_tasks command to execute tasks that have been scheduled. There is no other way. Process_tasks checks the db regularly for scheduled tasks and executes them in the background.

In a local environment

Open a terminal, cd into your app folder and launch the command python manage.py process_tasks. If you are using a virtualenv make sure you activate it first.

In a production environment

Option 1: Run a cron job

One solution is to launch the process_tasks command using a cron job. Beware that cron launches a new process at the scheduled interval without caring about the previous processes. You must make sure the process ends by the time the new cron call is scheduled. Otherwise you will be wasting resources on parallel instances of process_tasks. Happily this check is fairly easy to do.

To launch process_tasks for a limited time you can use its duration parameter. For example, to launch it with a life-span of 10 hours you call:

python manage.py process_tasks --duration 36000 

Configure the cron job: In cPanel you can use the Cron Jobs app. Configure the cron job to run hourly and add the following lines as your cron command:

cd /home/username/appname && /home/username/virtualenv/bin/python /home/username/appname/manage.py process_tasks --duration 3540 

This will cd into your app folder and launch the process_tasks command, taking into consideration your virtual environment and using a life-span of 59 minutes for the process_tasks command.

Update the paths to you app & virtual environment accordingly! Adjust the cron job interval and duration parameter to fit your needs!

Benefits:

  • If the command fails it will get restarted by the cron job later.
  • You don't have to worry about resource/memory leaks too much.

Option 2. Launch command at server startup

Basically you will have to create a script file containing the same launch command and configure your server to run it at startup.

You lose the benefit that your process is restarted from time to time so you will probably have to monitor its health state. If you are going down this path, a good approach is to use supervisord to monitor your process.

answered Jun 21, 2020 at 7:03

Comments

0

That is how django-background-tasks works. You have to run that process for the task runner to start processing tasks.

https://github.com/arteria/django-background-tasks#running-tasks

answered Feb 7, 2017 at 17:13

4 Comments

Can I do background task without executing python manage.py process_tasks command ? ? Or can I automate that process ?
From what I can tell in the documentation you have to run the command. I use this in production at my work. We run our Django application process (uwsgi or gunicorn or something like that) and then we run the process_tasks command that churns through the tasks.
Can you explain more about how you're activating the background-tasks process queue as per the op's question. I've got the same question. The only clear answers I've found are similar to Faiya's above, either running via a separate bash script or executing the manage.py command directly and leaving it live via tmux or something similar. I would like to know, however, if there is a more direct way to call the manage.py command via the wsgi.py file or somewhere else within the django stack. It sounds like you've got experience and any suggestions would be appreciated.
I've done this in two ways. uwsgi can run cron jobs for you. I like the cron2 syntax in the configs - uwsgi-docs.readthedocs.io/en/latest/… . An example config for this might be cron2 = minute=-1 python path/to/manage.py process_tasks -d 30 This will run the task for 30 seconds every minute. Another way I've done this is using Supervisor. supervisord.org
0

You can call the manage.py command from directly in your django project using the below code:

from django.core.management import call_command
call_command('process_tasks')

check out the docs here

answered Dec 15, 2018 at 17:04

1 Comment

Doesn't work in this case. You will get django.core.management.base.CommandError: Unknown command
0

I am also trying to use Django Background Tasks and have done successfully everything and my task execute perfectly when run manually "python manage.py process_tasks" but my background task does not trigger as per @background(schedule=60) automatically without running "python manage.py process_tasks" or whatever I set schedule time. than what is the point of setting schedule time for a task. When task has to be executed with command python manage.py process_task is mandatory to trigger all background task.

answered May 4, 2019 at 15:54

1 Comment

Did yo ever find a away to run it automatically
0

python manage.py process_tasks &

gunicorn --workers=3 app.wsgi -b 0.0.0.0:8080

answered Apr 10, 2020 at 3:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.