Django Channels 与 Redis 在 WSL2 中

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

django channels with redis in WSL2

问题

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

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

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

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

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

  1. $ python3 manage.py shell
  2. Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. (InteractiveConsole)
  5. import channels.layers
  6. channel_layer = channels.layers.get_channel_layer()
  7. from asgiref.sync import async_to_sync
  8. async_to_sync(channel_layer.send)('test_channel', {'type': 'hello'})
  9. async_to_sync(channel_layer.receive)('test_channel')

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

  1. Task exception was never retrieved
  2. 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')>
  3. Traceback (most recent call last):
  4. File ...\venv\lib\site-packages\redis\asyncio\connection.py, line 732, in disconnect
  5. self._writer.close() # type: ignore[union-attr]
  6. File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\streams.py", line 337, in close
  7. return self._transport close()
  8. File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\selector_events.py", line 706, in close
  9. self._loop.call_soon(self._call_connection_lost, None)
  10. 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
  11. self_check_closed()
  12. 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
  13. raise RuntimeError('Event loop is closed')
  14. RuntimeError: Event loop is closed
  15. {'type': 'hello'}

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

  1. ASGI_APPLICATION = "sst4.asgi.application"
  2. CHANNEL_LAYERS = {
  3. "default": {
  4. "BACKEND": "channels_redis.core.RedisChannelLayer",
  5. "CONFIG": {
  6. "hosts": [("127.0.0.1", 6379)],
  7. },
  8. },
  9. }
英文:

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:

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

which leads inside of WSL2 to:

  1. 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:

  1. $ python3 manage.py shell
  2. Python 3.10.9 (tags/v3.10.9:1dd9be6, Dec 6 2022, 20:01:21) [MSC v.1934 64 bit (AMD64)] on win32
  3. Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  4. (InteractiveConsole)
  5. import channels.layers
  6. channel_layer = channels.layers.get_channel_layer()
  7. from asgiref.sync import async_to_sync
  8. async_to_sync(channel_layer.send)(&#39;test_channel&#39;, {&#39;type&#39;: &#39;hello&#39;})
  9. async_to_sync(channel_layer.receive)(&#39;test_channel&#39;)

the last call results in the following error:

  1. Task exception was never retrieved
  2. 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;
  3. Traceback (most recent call last):
  4. File ...\venv\lib\site-packages\redis\asyncio\connection.py&quot;, line 732, in disconnect
  5. self._writer.close() # type: ignore[union-attr]
  6. 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
  7. return self._transport.close()
  8. 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
  9. self._loop.call_soon(self._call_connection_lost, None)
  10. 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
  11. self._check_closed()
  12. 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
  13. raise RuntimeError(&#39;Event loop is closed&#39;)
  14. RuntimeError: Event loop is closed
  15. {&#39;type&#39;: &#39;hello&#39;}

I configured my channels in settings.py:

  1. ASGI_APPLICATION = &quot;sst4.asgi.application&quot;
  2. CHANNEL_LAYERS = {
  3. &quot;default&quot;: {
  4. &quot;BACKEND&quot;: &quot;channels_redis.core.RedisChannelLayer&quot;,
  5. &quot;CONFIG&quot;: {
  6. &quot;hosts&quot;: [(&quot;127.0.0.1&quot;, 6379)],
  7. },
  8. },
  9. }

答案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:

确定