django+celery+redis错误

python——3.7

django——2.1

centos——7

redis——4.0.6

celery——4.3

1、配置redis.conf文件

celery和redis在centos虚拟机上,

所以redis需要设置远程连接

注释#127.0.0.1表示为任何ip地址可以访问

修改为no表示关闭安全模式

daemonize表示守护进程,默认为no需改为yes

错误1:

error) DENIED Redis is running in protected mode 
because protected mode is enabled, no bind address 
was specified, no authentication password is requested
 to clients, In thi mode connections are only accepted
 from the loopback interface. If you want to connec from
 external computers to Redis you may adopt one of 
the following solutions: 1) Jus.disable protected mode 
sending the command 'CONEIG SET protected-mode 
no' from the Le opback interface by connecting to Redis
 from the same host the server is running, howey er
 MAKE SURE Redis is not publicly accessible from 
internet if you do so. Use CONFIG RE WRITE to make
 this change permanent. 2) Alternatively you can 
just disable the protecte d mode by editing the Redis 
confiquration file, and setting the protected mode 
option t o 'no', and then restarting the server. 3) If
 you started the server manually just for testing, 
restart it with the '--protected-mode no' option, 4) 
Setup a bind address or al n authentication password. 
NOTE: You only need to do one of the above things in order 
f or the server to start accepting connections from the outside.

以上操作完成,连接时还是报错,原因是启动redis时没有启动对应的配置文件

解决:

启动成功:

2、django运行

# 使用celery
from django.conf import settings
from django.core.mail import send_mail
from celery import Celery
import time
# 创建一个Celery类的实例对象
app = Celery('celery_tasks.tasks',broker='redis://ip地址:6379/8')

#定义任务函数
@app.task
def send_register_active_email(to_email,username,token):
    '''发送激活邮件'''
    #组织邮件信息
    subject = "天天生鲜欢迎信息"
    message = ""  # 邮件正文
    sender = settings.EMAIL_FROM
    receiver = [to_email]  # 收件人邮箱列表
    html_message = '<h1>%s, 欢迎您成为天天生鲜注册会员</h1>请点击下面链接激活您的账户<br/><a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>' % (
    username, token, token)
    send_mail(subject, message, sender, receiver, html_message=html_message)
    time.sleep(7)

错误2:

两种错误一个问题所致。网上所说async在Python3.7已经是关键字了,但是celery版本没有更新导致的。

我们只要修改redis.py文件中报错的async改为另外的字符即可。比如我修改为了async_1。

只修改报错的即可

解决:

    def _create_client(self, async_1=False):
        if async_1:
            return self.asyncClient(connection_pool=self.async_pool)
        return self.Client(connection_pool=self.pool)

    def _get_pool(self, async_1=False):
        params = self._connparams(async_1=async_1)
        self.keyprefix_fanout = self.keyprefix_fanout.format(db=params['db'])
        return redis.ConnectionPool(**params)

运行成功并未报错

原文地址:https://www.cnblogs.com/xiaotaiyangi/p/10869209.html