Python 3.7.0 + Django 2.0.4 pits encountered using django-celery

Keywords: Celery Django pip Redis

First of all, you know that Django is a synchronization framework.
In order to speed up user response time and improve user experience. So I decided to use asynchronous tasks to perform some tasks in the background.
In addition to asynchronous tasks, celery can also start timing tasks to facilitate scheduling.

Packages to be installed
Better to install in sequence

  pip install celery

  pip install celery-with-redis

  pip install django-celery

Because the word async already exists as a system keyword in Python version 3.7 or more.
So you have to change all the files that involve this keyword.






That's basically all. There are many keywords in one file. I remember 27 keywords. Be careful when replacing them.

Next, set the file

      # End initialization
  import djcelery
  BROKER_URL = 'redis://'
  CELERY_IMPORTS = ('apply name.task')


  #Importing Asynchronous Tasks
  from celery.task import task
  #Import Timing Task Library
  from celery.decorators import periodic_task
  #Setting Task Cycle with Parameters
  def some_task():
      print('Execute every 10 seconds')
      print('completion of enforcement')
      return True

  #Register Asynchronous Tasks with Decorators
  # sendmail here is imported from other files, so you can implement some functions by yourself.
  def task_mail():
      #Instantiate an object
      sendmail = SendMail(
      'Welcome to register',
      'Your verification code is 1234.',
      ['Mail box number'],
    status = sendmail.do_send_mail()
    if status:
        print('Successful email delivery')
        print('Failed to send mail')


#Guide bag
import os
import django
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
app = Celery('project')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

def debug_task(self):
    print('Request: {0!r}'.format(self.request))

Startup service

#Asynchronous service
  celery -A Project name worker -l info  
#Timing Task Service
  celery -A myproject beat -l info

However, when performing asynchronous tasks, it is found that services are always automatically disconnected.
I think it's because the redis version in the python library is too high
So uninstall through pip, and then specify to install low version 2.6.10

pip uninstall redis
pip install redis==2.6.10

The basic process is like this, do not like light spray.

Posted by forgun on Wed, 20 Mar 2019 15:39:25 -0700