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.

  /kombu/async

 /celery/utils/timer2.py

 /concurrency/asynpool.py

 /kombu/transport/redis.py

 /celery/worker/auto_scale.py,components,consumer,strategy

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

Next, set the settings.py file

  INSTALLED_APPS = (
   ...,
   ...,
   ...,
   ...,
   ...,
   'djcelery'
  }
  
      # End initialization
  import djcelery
  djcelery.setup_loader()
  BROKER_URL = 'redis://127.0.0.1:6379/0'
  CELERY_IMPORTS = ('apply name.task')

Add task.py


  #Importing Asynchronous Tasks
  from celery.task import task
  #Import Timing Task Library
  from celery.decorators import periodic_task
  
  #Setting Task Cycle with Parameters
  @periodic_task(run_every=10)
  def some_task():
      print('Execute every 10 seconds')
      time.sleep(5)
      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.
  @task
  def task_mail():
      #Instantiate an object
      sendmail = SendMail(
      'Welcome to register',
      'Your verification code is 1234.',
      ['Mail box number'],
      DEFAULT_FROM_EMAIL
      )
    status = sendmail.do_send_mail()
    if status:
        print('Successful email delivery')
    else:
        print('Failed to send mail')

New celery.py

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

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

@app.task(bind=True)
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