我按照http://docs.celeryproject.org/en/master/django/first-steps-with-django.html
上的教程进行操作from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cobros.settings')
app = Celery('cobros')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
# Settings
CELERY_APP="cobros"
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
并致电我的第一份工作:
@task
def add(x, y):
logger.info("Sum ", x, y)
return x + y
def syncClientAsync():
baseDir = getBaseDir(str(cobrador.id))
print("Delay...")
return add.delay(4, 4) #No work
return add.delay(4, 4).get(timeout=1) #No work
当我运行代码时,python会永远卡住/阻塞。 Redis和Celery正在运行,不报告任何错误或问题。