SoFunction
Updated on 2024-10-30

Django in the use of celery to complete the asynchronous task sample code

This article mainly introduces how to use celery in django to complete asynchronous tasks, web projects in order to improve the user experience can be put on some time-consuming operations to the asynchronous queue to execute, such as activation of the mail, the background calculation operations, etc. The current project environment is: django==1.11.8 celery==3.1.25 redis==2.10.6 pip==9.0.1 python3==3.5.2 django-celery==3.1.17

First, create a Django project and celery configuration

1, create Django project

1> Open a terminal and type: django-admin startproject TestCelery Create django project ('TestCelery' is the name of the project)

2> Perform TestCelery in the terminal enter the command: django-admin startapp testcelery Create the application ('testcelery is the application name')

2. Set the environment variable for celery.

1> In the project create a file in TestCelery (with the same level) enter the following:

from celery import Celery
from  import settings
import os

# Setting environment variables for celery
('DJANGO_SETTINGS_MODULE', '')

# Create applications
app = Celery('testcelery')

# Acid configuration applications
(
  
  # Local Redis server
  BROKER_URL='redis://127.0.0.1:6379/2',
)

app.autodiscover_tasks(settings.INSTALLED_APPS)

2> The current project directory is shown below:

                                 

Second, the creation of tasks tasks, write a view View and urls

1, Create a new file in the testcelery application and write the task to be processed:

from  import app
from time import sleep
@
def start_running(nums):
  print('***>%s<***' %nums)
  print('---->> > Starting task <<----')
  for i in range(10):
    print('>>'*(i+1))
    sleep(1)
  print('>---End of mission--<')

2, write the view view, and write the method to call the client

from  import View
from  import HttpResponse
from .tasks import start_running
from time import sleep
# Create your views here.

class IdexView(View):
  def get(self, request):
    print('>=====Start sending requests=====<')
    for i in range(10):
      print('>>',end='')
      sleep(0.1)

    start_running.delay('》》》》》 I'm transmitting this 《《《《《')
    return HttpResponse('<h2> Request sent </h2>')

3, write usrls for testcelery application

from  import url
from .views import *
urlpatterns = [
  url(r'^$', IdexView.as_view()),
]

4, the current project directory is shown below:

 

Third, run the project, open the worker

1, Run the project under the current project and enter the command to start the service: python runserver, the following figure shows that the run is successful:

2, open another terminal under the current project, enter the command: celery -A TestCelery worker --loglevel=DEBUG, start the following as shown:

 

3, Calling Tasks

1> Open your browser and type inhttp://127.0.0.1:8000/send/ conduct an interview

2> When woker listens for a task request, it executes the time-consuming task as shown below:

This is the entire content of this article.