【问题标题】:Why is channel layer not communicating with Redis in my django project?为什么我的 django 项目中通道层不与 Redis 通信?
【发布时间】:2020-04-23 06:25:18
【问题描述】:

大家好,我是频道和 Redis 的新手,我尝试按照频道文档的这一部分进行操作,上面写着:

让我们确保通道层可以与 Redis 通信。打开 Django shell 并运行以下命令 命令:

$ python3 manage.py shell
>>> import channels.layers

>>> channel_layer = channels.layers.get_channel_layer()

>>> from asgiref.sync import async_to_sync

>>> async_to_sync(channel_layer.send)('test_channel', {'type': 'hello'})

>>> async_to_sync(channel_layer.receive)('test_channel'){'type': 'hello'}

但我遇到了这个错误;

>>> import channels.layers
>>> channel_layer = channels.layers.get_channel_layer()
>>> from asgiref.sync import async_to_sync

>>> async_to_sync(channel_layer.send)('test_channel', {'type': 'hello'})
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\asgiref\sync.py", line 116, in __call__
    return call_result.result()
  File "C:\Python37\Lib\concurrent\futures\_base.py", line 428, in result
    return self.__get_result()
  File "C:\Python37\Lib\concurrent\futures\_base.py", line 384, in __get_result
    raise self._exception
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\asgiref\sync.py", line 156, in main_wrap
    result = await self.awaitable(*args, **kwargs)
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\channels_redis\core.py", line 293, in send
    async with self.connection(index) as connection:
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\channels_redis\core.py", line 820, in __aenter__
    self.conn = await self.pool.pop()
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\channels_redis\core.py", line 70, in pop
    conns.append(await aioredis.create_redis(**self.host, loop=loop))
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\aioredis\commands\__init__.py", line 175, in create_redis
    loop=loop)
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\aioredis\connection.py", line 113, in create_connection
    timeout)
  File "C:\Python37\Lib\asyncio\tasks.py", line 414, in wait_for
    return await fut
  File "C:\Users\LENOVO\Project\BuildUp\lib\site-packages\aioredis\stream.py", line 24, in open_connection
    lambda: protocol, host, port, **kwds)
  File "C:\Python37\Lib\asyncio\base_events.py", line 954, in create_connection
    raise exceptions[0]
  File "C:\Python37\Lib\asyncio\base_events.py", line 941, in create_connection
    await self.sock_connect(sock, address)
  File "C:\Python37\Lib\asyncio\selector_events.py", line 464, in sock_connect
    return await fut
  File "C:\Python37\Lib\asyncio\selector_events.py", line 494, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 10061] Connect call failed ('127.0.0.1', 6379)

我的 INSTALLED_APPS 中有“频道”,我的设置中也有 CHANNEL_LAYERS;

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    },
}

我正在使用 python 3.7.5,当我输入“pip freeze”命令时,我看到以下内容;

aioredis==1.3.1
asgiref==3.2.3
async-timeout==3.0.1
attrs==19.3.0
autobahn==19.11.1
Automat==0.8.0
cffi==1.13.2
channels==2.4.0
channels-redis==2.4.1
constantly==15.1.0
cryptography==2.8
daphne==2.4.1
Django==3.0.1
django-countries==5.5
django-crispy-forms==1.8.1
djangorestframework==3.11.0
hiredis==1.0.1
hyperlink==19.0.0
idna==2.8
incremental==17.5.0
msgpack==0.6.2
Pillow==6.2.1
pyasn1==0.4.8
pyasn1-modules==0.2.7
pycparser==2.19
PyHamcrest==1.9.0
pyOpenSSL==19.1.0
pytz==2019.3
service-identity==18.1.0
six==1.13.0
sqlparse==0.3.0
Twisted==19.10.0
txaio==18.8.1
zope.interface==4.7.1

【问题讨论】:

  • 你有redis的实例在运行吗?
  • 我不确定,我刚刚用 pip 安装了 redis,并将 CHANNEL_LAYERS 设置粘贴到 settings.py.... 还有什么我应该做的吗?

标签: python django redis channel django-channels


【解决方案1】:

你是这样运行redis实例的吗?

docker run -p 6379:6379 -d redis:5

使用提示检查:

docker container ls

您应该会看到一个 redis 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 2022-08-19
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    相关资源
    最近更新 更多