django 尝试启动服务器时检测到死锁

lyfkaqu1  于 2023-06-25  发布在  Go
关注(0)|答案(3)|浏览(178)

我有一个简单的应用程序,我同时使用django和django-rest-framework。
通常,当我尝试启动本地服务器(python manage.py runserver)时,会出现以下异常:

Watching for file changes with StatReloader
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 66, in __getitem__
    return self._engines[alias]
KeyError: 'django'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "/project/venv/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/project/venv/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/project/venv/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/project/venv/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/project/venv/lib/python3.7/site-packages/django/contrib/admin/checks.py", line 80, in check_dependencies
    for engine in engines.all():
  File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 90, in all
    return [self[alias] for alias in self]
  File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 90, in <listcomp>
    return [self[alias] for alias in self]
  File "/project/venv/lib/python3.7/site-packages/django/template/utils.py", line 81, in __getitem__
    engine = engine_cls(params)
  File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 25, in __init__
    options['libraries'] = self.get_templatetag_libraries(libraries)
  File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 43, in get_templatetag_libraries
    libraries = get_installed_libraries()
  File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 108, in get_installed_libraries
    for name in get_package_libraries(pkg):
  File "/project/venv/lib/python3.7/site-packages/django/template/backends/django.py", line 121, in get_package_libraries
    module = import_module(entry[1])
  File "/project/venv/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/project/venv/lib/python3.7/site-packages/rest_framework/templatetags/rest_framework.py", line 15, in <module>
    from rest_framework.renderers import HTMLFormRenderer
  File "/project/venv/lib/python3.7/site-packages/rest_framework/renderers.py", line 20, in <module>
    from django.test.client import encode_multipart
  File "/project/venv/lib/python3.7/site-packages/django/test/client.py", line 23, in <module>
    from django.test import signals
  File "<frozen importlib._bootstrap>", line 980, in _find_and_load
  File "<frozen importlib._bootstrap>", line 149, in __enter__
  File "<frozen importlib._bootstrap>", line 94, in acquire
_frozen_importlib._DeadlockError: deadlock detected by _ModuleLock('django.test.signals') at 4420467792

Performing system checks...

在几次重试之后,服务器成功启动。因此,它不是一个显示停止,但它是相当恼人的。
由于我是Django的新手,我想知道是否有一种方法可以防止这样的错误。

omjgkv6w

omjgkv6w1#

我也有同样的问题,我还不知道如何重现它,为什么会发生。但它看起来像是与新版本的Django有关的东西。
从这个问题的评论来看,它看起来像是与虚拟环境中的.pyc文件相关的问题。
为了解决这个问题,我是这样做的:

  • 使用以下命令删除项目中的所有.pyc文件:find . | grep -E "(/__pycache__$|\.pyc$|\.pyo$)" | xargs rm -rf
  • 已停用虚拟环境并重新激活。

问题消失了

qrjkbowd

qrjkbowd2#

我遇到了同样的问题,无法可靠地复制它。我试图删除我的.pyc文件,但仍然有同样的问题。我把'rest_framework'移到了INSTALLED_APPS列表的末尾,这似乎对我很有效。

kuhbmx9i

kuhbmx9i3#

该错误是由于Django Rest Framework,您可以通过删除它并看到错误消失来检查此错误。更具体地说,在我的例子中,简单地移除

from rest_framework.urlpatterns import format_suffix_patterns

urls.py解决了这个问题,仍然安装了Django Rest Framework。

相关问题