简体   繁体   English

Heroku / Redis Python/Django 项目连接错误

[英]Heroku / Redis Connection error on Python/Django Project

I'm trying to set up django so it send automatic email when a certain date in my models i reached.我正在尝试设置 django,以便在我到达模型中的某个日期时自动发送 email。 However i setup a Heroku-Redis server and am trying to connect to it.但是我设置了一个 Heroku-Redis 服务器并试图连接到它。 I created a simple task to test out if celery is working but it always returns the following error:我创建了一个简单的任务来测试 celery 是否正常工作,但它总是返回以下错误:

Error while reading from socket: (10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)从套接字读取时出错:(10054,“现有连接被远程主机强行关闭”,无,10054,无)

I setup celery according to the website:我根据网站设置celery:

celery.py: celery.py:

import os

from celery import Celery

# Set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'incFleet.settings')

app = Celery('incFleet')

# 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 apps.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print(f'Request: {self.request!r}')

Tasks.py任务.py

import datetime    
from celery import shared_task, task
from time import sleep
from .models import trucks
from datetime import datetime


@shared_task()
def sleepy(duration):
    sleep(duration)
    return 0

#@shared_task()
#def send_warning():

My views:我的看法:

def index(request):
    sleepy.delay(5)
    return render(request, 'Inventory/index.html')

And my settings.py还有我的设置.py

# Celery Broker - Redis
CELERY_BROKER_URL = 'redis://:p0445df1196b44ba70a9bd0c84545315fec8a5dcbd77c8e8c4bd22ff4cd0a2ff4@ec2-54-167-58-171.compute-1.amazonaws.com:11900/'
    CELERY_RESULT_BACKEND = 'redis://:p0445df1196b44ba70a9bd0c84545315fec8a5dcbd77c8e8c4bd22ff4cd0a2ff4@ec2-54-167-58-171.compute-1.amazonaws.com:11900/'
    CELERY_ACCEPT_CONTENT = ['json']
    CELERY_TASK_SERIALIZER = 'json'

Heroku recommends using encryption and thus a rediss:// URL instead of redis:// Heroku 建议使用加密,因此使用rediss:// URL 而不是redis://

Source - https://stackoverflow.com/a/68730097/14018892来源 - https://stackoverflow.com/a/68730097/14018892

After a while i realized the issue was with the network in my office.过了一会儿,我意识到问题出在我办公室的网络上。 The port i was trying to access was closed.我试图访问的端口已关闭。 As soon as i activated redis on the actual server it started working fine.一旦我在实际服务器上激活 redis,它就开始正常工作。

heroku config | grep REDIS

check the actual url using this code使用此代码检查实际的 url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM