【问题标题】:Celery + Redis - Django blocks when triggering a task with delayCelery + Redis - Django 在延迟触发任务时阻塞
【发布时间】:2019-08-03 09:19:56
【问题描述】:

我已经使用 Redis 在我的 Django 项目中设置了 Celery。 计划的任务正在运行,没有问题。 使用delay() 触发异步任务时会出现问题。执行停止,就像在kombu.utils.retry_over_time的循环中被阻塞了一样。

我检查了一下,Redis 已启动并正在运行。 我真的不知道如何调试这个问题。

这是一些软件包版本

Django==2.1.2
celery==4.2.1
django-celery-beat==1.4.0
django-celery-results==1.0.4
redis==3.2.0
kombu==4.4.0

设置

CELERY_REDIS_HOST = 'localhost'
CELERY_REDIS_PORT = 6379
CELERY_REDIS_DB = 1 # # Redis DB number, if not provided the default will be 0
CELERY_REDIS_PASSWORD = ''

CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'

CELERY_BROKER_URL = 'redis://{host}:{port}/{db}'.format(host=CELERY_REDIS_HOST, port=CELERY_REDIS_PORT, db=CELERY_REDIS_DB)
CELERY_RESULT_BACKEND = 'django-db'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json' # Result serialization format
CELERY_TASK_SERIALIZER = 'json' # String identifying the serializer to be used

CELERY_BROKER_TRANSPORT_OPTIONS = {
    'visibility_timeout': 3600, # 1 hour, default Redis visibility timeout
}

Celery 和 Celery Beat 是如何推出的

将 Celery 和 Celery Beat 添加到 Supervisor 的 Shell 脚本:

#!/usr/bin/env bash

# Create required directories
sudo mkdir -p /var/log/celery/
sudo mkdir -p /var/run/celery/

# Create group called 'celery'
sudo groupadd -f celery
# add the user 'celery' if it doesn't exist and add it to the group with same name
id -u celery &>/dev/null || sudo useradd -g celery celery
# add permissions to the celery user for r+w to the folders just created
sudo chown -R celery:celery /var/log/celery/
sudo chown -R celery:celery /var/run/celery/

# Get django environment variables
celeryenv=`cat ./env_vars | tr '\n' ',' | sed 's/%/%%/g' | sed 's/export //g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PYTHONPATH//g' | sed 's/$LD_LIBRARY_PATH//g'`
celeryenv=${celeryenv%?}

# Create CELERY configuration script
celeryconf="[program:celeryd]
directory=/home/ubuntu/splityou/splityou
; Set full path to celery program if using virtualenv
command=/home/ubuntu/splityou/splityou-env/bin/celery worker -A config.celery.celery_app:app --loglevel=INFO --logfile=\"/var/log/celery/%%n%%I.log\" --pidfile=\"/var/run/celery/%%n.pid\"

user=celery
numprocs=1
stdout_logfile=/var/log/celery-worker.log
stderr_logfile=/var/log/celery-worker.log
autostart=true
autorestart=true
startsecs=10

; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 60

; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true

; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=998

environment=$celeryenv"


# Create CELERY BEAT configuration script
celerybeatconf="[program:celerybeat]
; Set full path to celery program if using virtualenv
command=/home/ubuntu/splityou/splityou-env/bin/celery beat -A config.celery.celery_app:app --loglevel=INFO --logfile=\"/var/log/celery/celery-beat.log\" --pidfile=\"/var/run/celery/celery-beat.pid\"

directory=/home/ubuntu/splityou/splityou
user=celery
numprocs=1
stdout_logfile=/var/log/celerybeat.log
stderr_logfile=/var/log/celerybeat.log
autostart=true
autorestart=true
startsecs=10

; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 60

; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true

; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=999

environment=$celeryenv"

# Create the celery supervisord conf script
echo "$celeryconf" | tee /etc/supervisor/conf.d/celery.conf
echo "$celerybeatconf" | tee /etc/supervisor/conf.d/celerybeat.conf

# Enable supervisor to listen for HTTP/XML-RPC requests.
# supervisorctl will use XML-RPC to communicate with supervisord over port 9001.
# Source: https://askubuntu.com/questions/911994/supervisorctl-3-3-1-http-localhost9001-refused-connection
if ! grep -Fxq "[inet_http_server]" /etc/supervisor/supervisord.conf
  then
    echo "[inet_http_server]" | tee -a /etc/supervisor/supervisord.conf
    echo "port = 127.0.0.1:9001" | tee -a /etc/supervisor/supervisord.conf
fi

# Reread the supervisord config
sudo supervisorctl reread

# Update supervisord in cache without restarting all services
sudo supervisorctl update

# Sleep for 15 seconds to give enough time to previous supervisor instance to shutdown
# Source: https://stackoverflow.com/questions/50135628/celery-django-on-elastic-beanstalk-causing-error-class-xmlrpclib-fault/50154073#50154073
sleep 15

# Start/Restart celeryd through supervisord
sudo supervisorctl restart celeryd
sudo supervisorctl restart celerybeat

【问题讨论】:

  • 你得到堆栈跟踪了吗?
  • 你在运行 celerycelery beat 吗?
  • 我遇到了类似的问题。这是因为我忘记在后台运行 celery/celery beat
  • @IainShelvington 不,我没有得到任何堆栈跟踪。
  • @Pythonista 是的,我正在运行 celery worker 和 celery beat。我刚刚添加了主管命令

标签: django redis celery


【解决方案1】:

正如First steps with Django Celery 教程中所指出的,我们必须在proj/__init__.py 模块中导入应用程序对象。 它确保在 Django 启动时始终导入应用程序,以便 shared_task 使用相同的内容。

我完全忘记了,所以我通过在__init__.py 中输入以下内容来解决问题:

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ('celery_app',)

【讨论】:

  • 我还发现,工作应用程序中的命名空间、celery.py 的位置和您的 django installed_apps 字符串必须匹配一些,当然没有记录的要求。否则,当然,您在尝试调用 task.delay 时会收到一个静默错误/挂起。这里有漂亮的技术,喜欢他们所拥有的“猜测它如何在没有文档的情况下工作”的理念。
猜你喜欢
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2013-03-01
  • 2011-03-22
  • 2014-11-24
  • 2012-01-22
  • 2019-10-23
相关资源
最近更新 更多