从 django urls 运行的 websockets 错误:RuntimeError: There is no current event loop in thread 'Dummy-1'

Posted

技术标签:

【中文标题】从 django urls 运行的 websockets 错误:RuntimeError: There is no current event loop in thread \'Dummy-1\'【英文标题】:websockets running from django urls error: RuntimeError: There is no current event loop in thread 'Dummy-1'从 django urls 运行的 websockets 错误:RuntimeError: There is no current event loop in thread 'Dummy-1' 【发布时间】:2019-01-03 12:19:07 【问题描述】:

我正在尝试从 django 运行这个 websocket 脚本

async def get_cells(websocket, path):
  async for message in websocket:

    message = json.loads(message)
    ...
            if percentage % 5 == 0:
                await websocket.send(json.dumps('complete': False, 'status': percentage))
    ...

        await websocket.send(json.dumps('complete': True, 'filename': filename))

asyncio.get_event_loop().run_until_complete( websockets.serve(get_cells, 'localhost', 8765))

asyncio.get_event_loop().run_forever()

我保存为 path.py 并在导入的 url 中

import path

抛出这个错误

^C(pixelart) sam@sam-Lenovo-G51-35:~/code/pixelart$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f38e007f1e0>
Traceback (most recent call last):
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/core/management/commands/runserver.py",line 117, in inner_run
    self.check(display_num_errors=True)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/urls/resolvers.py", line 396, in check
    for pattern in self.url_patterns:
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
File "/home/sam/code/envs/pixelart/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
File "/home/sam/code/envs/pixelart/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/sam/code/pixelart/pixelart/urls.py", line 7, in <module>
    import path
File "/home/sam/code/pixelart/path.py", line 55, in <module>
    asyncio.get_event_loop().run_until_complete(
File "/usr/lib/python3.6/asyncio/events.py", line 694, in get_event_loop
    return get_event_loop_policy().get_event_loop()
File "/usr/lib/python3.6/asyncio/events.py", line 602, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Dummy-1'.

建议

loop = asyncio.get_event_loop().run_until_complete(
    websockets.serve(get_cells, 'localhost', 8765))

asyncio.get_event_loop(loop).run_forever()

此建议返回相同的错误。

【问题讨论】:

【参考方案1】:

您可能根据 path.py 文件所在的位置放置了 URL 导入的路径。

你必须根据manage.py文件所在的位置放置路径。

希望对你有帮助!

【讨论】:

是的,我实际上忘记保存对 urls.py 的更改,这就是它无法正常工作或引发任何错误的原因。现在它实际上是在扔一个。 您是否在程序的其他任何地方使用了调度程序?【参考方案2】:

尝试保存

asyncio.get_event_loop()

在变量中,比如

var = asyncio.get_event_loop()

并使用

asyncio.set_event_loop(var)

【讨论】:

对不起。最后你必须使用“set_event_loop” 即使进行了更改,它仍然返回相同的错误。

以上是关于从 django urls 运行的 websockets 错误:RuntimeError: There is no current event loop in thread 'Dummy-1'的主要内容,如果未能解决你的问题,请参考以下文章

beego与websocker的集成

ImportError:升级到 Django 4.0 后无法从“django.conf.urls”导入名称“url”

从 django urls 运行的 websockets 错误:RuntimeError: There is no current event loop in thread 'Dummy-1'

django搭建完毕运行显示hello django

Django 从 3.2 -> 4.0 和 DRF 路径更新

Android websock 应用