我正在尝试从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,并在导入的网址中
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()
此建议返回相同的错误。
答案 0 :(得分:0)
您可能已经根据path.py文件的位置放置了URL导入的路径。
您必须根据manage.py文件所在的位置放置路径。
希望有帮助!
答案 1 :(得分:0)
尝试并保存
asyncio.get_event_loop()
在变量中,例如
var = asyncio.get_event_loop()
并使用
asyncio.set_event_loop(var)