Django-Channels失败,同时给出名称或服务未知错误

时间:2018-06-27 12:23:04

标签: django django-channels

我有一个相当简单的django-channels / daphne / asgi / redis应用程序。使用chrome时,该应用程序永远不会出现任何问题,但是当使用firefox(最新版本〜60)时,该应用程序将失败。我目前有以下错误:

api_1        | 2018-06-12 16:02:24,378 INFO     In the connect method
api_1        | xxxxxx:41647 - - [12/Jun/2018:16:02:24] "WSCONNECT /updates/" - -
api_1        | 2018-06-12 16:02:25,049 ERROR    Exception inside application: [Errno -2] Name or service not known
api_1        |   File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 54, in __call__
api_1        |     await await_many_dispatch([receive, self.channel_receive], self.dispatch)
api_1        |   File "/usr/local/lib/python3.6/site-packages/channels/utils.py", line 48, in await_many_dispatch
api_1        |     await dispatch(result)
api_1        |   File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 110, in __call__
api_1        |     return await asyncio.wait_for(future, timeout=None)
api_1        |   File "/usr/local/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
api_1        |     return (yield from fut)
api_1        |   File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
api_1        |     result = self.fn(*self.args, **self.kwargs)
api_1        |   File "/usr/local/lib/python3.6/site-packages/channels/db.py", line 13, in thread_handler
api_1        |     return super().thread_handler(loop, *args, **kwargs)
api_1        |   File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 125, in thread_handler
api_1        |     return self.func(*args, **kwargs)
api_1        |   File "/usr/local/lib/python3.6/site-packages/channels/consumer.py", line 99, in dispatch
api_1        |     handler(message)
api_1        |   File "/usr/local/lib/python3.6/site-packages/channels/generic/websocket.py", line 19, in websocket_connect
api_1        |     self.connect()
api_1        |   File "./flypoll/consumers.py", line 146, in connect
api_1        |     async_to_sync(self.channel_layer.group_add)("flypoll_socket_clients", self.channel_name)
api_1        |   File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 64, in __call__
api_1        |     return call_result.result()
api_1        |   File "/usr/local/lib/python3.6/concurrent/futures/_base.py", line 432, in result
api_1        |     return self.__get_result()
api_1        |   File "/usr/local/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result
api_1        |     raise self._exception
api_1        |   File "/usr/local/lib/python3.6/site-packages/asgiref/sync.py", line 78, in main_wrap
api_1        |     result = await self.awaitable(*args, **kwargs)
api_1        |   File "/usr/local/lib/python3.6/site-packages/channels_redis/core.py", line 283, in group_add
api_1        |     async with self.connection(self.consistent_hash(group)) as connection:
api_1        |   File "/usr/local/lib/python3.6/site-packages/channels_redis/core.py", line 403, in __aenter__
api_1        |     self.conn = await aioredis.create_redis(**self.kwargs)
api_1        |   File "/usr/local/lib/python3.6/site-packages/aioredis/commands/__init__.py", line 174, in create_redis
api_1        |     loop=loop)
api_1        |   File "/usr/local/lib/python3.6/site-packages/aioredis/connection.py", line 107, in create_connection
api_1        |     timeout, loop=loop)
api_1        |   File "/usr/local/lib/python3.6/asyncio/tasks.py", line 339, in wait_for
api_1        |     return (yield from fut)
api_1        |   File "/usr/local/lib/python3.6/site-packages/aioredis/stream.py", line 19, in open_connection
api_1        |     lambda: protocol, host, port, **kwds)
api_1        |   File "/usr/local/lib/python3.6/asyncio/base_events.py", line 734, in create_connection
api_1        |     infos = f1.result()
api_1        |   File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
api_1        |     result = self.fn(*self.args, **self.kwargs)
api_1        |   File "/usr/local/lib/python3.6/socket.py", line 745, in getaddrinfo
api_1        |     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
api_1        |   [Errno -2] Name or service not known

它说connect方法有问题,但是connect方法如下:

def connect(self):
        """
            standard connect handler, accepts the connection, and sets up some basic parameters
            :return:
        """
        logging.info("In the connect method")
        self.accept()
        async_to_sync(self.channel_layer.group_add)("flypoll_socket_clients", self.channel_name)

该应用已被docker化,并托管在ec2上。 redis,python,django也被docker化了。我已经将模块(asgiref,txaio,twisted,channels,daphne等)更新为最新版本,但我无法摆脱它。

怎么了?

1 个答案:

答案 0 :(得分:0)

您的django容器似乎无法访问redis。

在django设置中,您必须具有以下内容:

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [('[redis-host-here]', 6379)],  # REPLACE redis host here
        },
    },
}

该主机必须可以从您的django容器中访问