为什么在我的Django项目中通道层不与Redis通信?

问题描述 投票:0回答:1

大家好,我是Channel和Redis的新手,我试图遵循Channel文档的这一部分,它说;

请确保渠道层可以与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 Frozen'命令时,我看到以下内容;

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
python django redis channel django-channels
1个回答
0
投票

您是否像这样运行redis实例?docker run -p 6379:6379 -d redis:5

使用以下方法检查:docker容器ls

您应该看到redis的实例。

© www.soinside.com 2019 - 2024. All rights reserved.