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)
運行成功並未報錯