Sample Code of using celery to complete asynchronous tasks in Django, djangocelery
This article describes how to use celery to complete asynchronous tasks in django. To improve user experience, some time-consuming operations can be executed in the asynchronous queue, such as activating emails, 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
1. Create a Django project and celery Configuration
1. Create a Django Project
1> open the terminal and enter django-admin startproject TestCelery to create the django Project ('testcelery 'is the project name)
2> run the following command on the TestCelery terminal: django-admin startapp testcelery to create an application ('testcelery is the application name ')
2. Set environment variables for celery
1> Create the celery. py file in TestCelery in the project (same as setting. py) and enter the following content:
From celery import Celeryfrom django. conf import settingsimport OS # Set the environment variable OS for celery. environ. setdefault ('django _ SETTINGS_MODULE ', 'testcelery. settings') # create an app = Celery ('testcelery ') # configure the app with acid. conf. update (# local Redis server BROKER_URL = 'redis: // 127.0.0.1: 6379/2 ',) app. autodiscover_tasks (settings. INSTALLED_APPS)
2> shows the current project directory:
2. Create task tasks and write View and urls
1. Create the tasks. py file in the testcelery application and write the task to be processed:
From TestCelery. celery import appfrom time import sleep@app.taskdef start_running (nums): print ('***> % s <***' % nums) print ('---> start executing the task <---') for I in range (10): print ('>' * (I + 1) sleep (1) print ('> --- task end --- <')
2. Write the view and write the method to call the client.
From django. views import Viewfrom django. http import HttpResponsefrom. tasks import start_runningfrom time import sleep # Create your views here. class IdexView (View): def get (self, request): print ('>==== start to send a request =====< ') for I in range (10): print ('>', end = '') sleep (0.1) start_running.delay ('"I sent the'') return HttpResponse ('
3. Compile usrls for the testcelery application.
from django.conf.urls import urlfrom .views import *urlpatterns = [ url(r'^$', IdexView.as_view()),]
4. shows the current project directory:
3. Run the project and enable worker
1. Run the project and enter the command to start the service under the current project: python manager. py runserver. If it appears, as shown in, it indicates that the operation is successful:
2. Enable worker and open another terminal in the current project. Enter the command celery-A TestCelery worker -- loglevel = DEBUG. The following figure shows how to enable worker:
3. Call a task
1> open your browser and enter http: // 127.0.0.1: 8000/send/for access.
2> when woker listens to the task request, it will execute time-consuming tasks, as shown in:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.