Django Channels 与 Redis 在 WSL2 中

huangapple go评论64阅读模式
英文:

django channels with redis in WSL2

问题

我在Windows子系统中运行了Redis,它能正常工作,但我无法从Django Channels连接到它。在WSL中启动了Redis,当我在Windows的普通终端和Python中执行以下操作时:

import redis
c = redis.Redis("localhost", 6379, 0)
c.keys("hello world")

这在WSL2中会产生以下输出:

1675861647.991521 [0 [::1]:32934] "KEYS" "hello world"

但是,当我尝试使用channels 4教程中的函数执行相同的操作时,出现了问题:

$ python3 manage.py shell
Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

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')

最后一次调用导致以下错误:

Task exception was never retrieved
future: <Task finished name='Task-5' coro=<Connection.disconnect() done, defined at ...\venv\lib\site-packages\redis\asyncio\connection.py:723> exception=RuntimeError('Event loop is closed')>
Traceback (most recent call last):
  File ...\venv\lib\site-packages\redis\asyncio\connection.py, line 732, in disconnect
    self._writer.close()  # type: ignore[union-attr]
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\streams.py", line 337, in close
    return self._transport close()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\selector_events.py", line 706, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 753, in call_soon
    self_check_closed()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
{'type': 'hello'}

我在settings.py中配置了我的channels

ASGI_APPLICATION = "sst4.asgi.application"
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("127.0.0.1", 6379)],
        },
    },
}
英文:

I have a redis installation running inside the windows subsystem for linux. It is working finde, but I cannot connect to it from django-channels. In my WSL I started redis and when using a normal terminal and python in Windows I can do for example:

import redis
c = redis.Redis(&quot;localhost&quot;, 6379, 0)
c.keys(&quot;hello world&quot;)

which leads inside of WSL2 to:

1675861647.991521 [0 [::1]:32934] &quot;KEYS&quot; &quot;hello world&quot;

But when I am trying to do the same thing with the functions from the channels 4 tutorial I get stuck:

$ python3 manage.py shell
Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec  6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32
Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.  
(InteractiveConsole)

import channels.layers
channel_layer = channels.layers.get_channel_layer()
from asgiref.sync import async_to_sync
async_to_sync(channel_layer.send)(&#39;test_channel&#39;, {&#39;type&#39;: &#39;hello&#39;})
async_to_sync(channel_layer.receive)(&#39;test_channel&#39;)

the last call results in the following error:

Task exception was never retrieved
future: &lt;Task finished name=&#39;Task-5&#39; coro=&lt;Connection.disconnect() done, defined at ...\venv\lib\site-packages\redis\asyncio\connection.py:723&gt; exception=RuntimeError(&#39;Event loop is closed&#39;)&gt;    
Traceback (most recent call last):
  File ...\venv\lib\site-packages\redis\asyncio\connection.py&quot;, line 732, in disconnect
    self._writer.close()  # type: ignore[union-attr]
  File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\streams.py&quot;, line 337, in close
    return self._transport.close()
  File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\selector_events.py&quot;, line 706, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py&quot;, line 753, in call_soon
    self._check_closed()
  File &quot;C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py&quot;, line 515, in _check_closed
    raise RuntimeError(&#39;Event loop is closed&#39;)
RuntimeError: Event loop is closed
{&#39;type&#39;: &#39;hello&#39;}

I configured my channels in settings.py:

ASGI_APPLICATION = &quot;sst4.asgi.application&quot;
CHANNEL_LAYERS = {
    &quot;default&quot;: {
        &quot;BACKEND&quot;: &quot;channels_redis.core.RedisChannelLayer&quot;,
        &quot;CONFIG&quot;: {
            &quot;hosts&quot;: [(&quot;127.0.0.1&quot;, 6379)],
        },
    },
}

答案1

得分: 1

应该使用RedisPubSubChannelLayer而不是后面你在上述评论中描述的RedisChannelLayer

而且,它是pubsub的子类,不是你后来在上面描述的core

英文:

It should be RedisPubSubChannelLayer instead of RedisChannelLayer.

And it is a subclass of pubsub not core as you described later on in the above comment.

huangapple
  • 本文由 发表于 2023年2月8日 21:14:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定