Error 111 connecting to localhost:6379. Connection refused. Django Heroku

后端 未结 12 1183
感动是毒
感动是毒 2020-12-28 12:33

I am able to run redis locally and everything works.

However when I deploy to heroku I get this error:

Error 111 connecting to localhost:6379. Conn         


        
12条回答
  •  太阳男子
    2020-12-28 13:28

    Right now Heroku automatically sets the environment variable REDIS_URL to URL + port.

    A convenient way to work with redis on heroku is to use a connection pool:

    settings.py

    import redis
    REDIS_DEFAULT_CONNECTION_POOL = redis.ConnectionPool.from_url(os.getenv('REDIS_URL', 'redis://localhost:6379/'))
    

    whererver.py

    from redis import Redis
    from myProject.settings import REDIS_DEFAULT_CONNECTION_POOL
    
    redis = Redis(connection_pool=REDIS_DEFAULT_CONNECTION_POOL)
    print(redis.keys())  # works
    

提交回复
热议问题