/admin/users/user/1/change/上的django运行时错误,单线程执行器已被使用,将死锁

avkwfej4  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(138)

我试着用django-channels制作一个聊天应用程序。但是当我创建一个新的django-chanels项目并在管理站点中编辑数据时,出现了RuntimeError。
我不确定我是否认为错误发生是因为我做了几个django-channels项目。
当我构建我的第一个项目时,WebSocket不工作,所以我用django-channels构建了另一个第二、第三、第四个项目。现在WebSocket已经连接,但是我在RuntimeError(错误消息:单线程执行器已是用户,将死锁)
我能做些什么来解决这个问题?
我的项目settings.py

import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-is!$&voe3y058!2sus9egmxh@d!$)=l&o8_vl=m8zz!ap+d#a#"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "users.apps.UsersConfig",
    "channels",
    "core.apps.CoreConfig",
    "broadcasts.apps.BroadcastsConfig",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "busker.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "busker.wsgi.application"

# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]

# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = "/static/"

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

ASGI_APPLICATION = "busker.asgi.application"

CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}

AUTH_USER_MODEL = "users.User"

我的项目asgi.py

import os

from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
import broadcasts.routing

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = ProtocolTypeRouter(
    {
        "http": get_asgi_application(),
        # Just HTTP for now. (We can add other protocols later.)
        "websocket": AuthMiddlewareStack(
            URLRouter(broadcasts.routing.websocket_urlpatterns)
        ),
    }
)

我的应用consumers.py

import json
from channels.generic.websocket import AsyncWebsocketConsumer

class ChatConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.room_group_name = "Test-Room"

        await self.channel_layer.group_add(self.room_group_name, self.channel_name)

        await self.accept()

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard(self.room_group_name, self.channel_name)

        print("Disconnected!")

    async def receive(self, text_data):
        receive_dict = json.loads(text_data)
        message = receive_dict["message"]

        await self.channel_layer.group_send(
            self.room_group_name, {"type": "send.message", "message": message}
        )

    async def send_message(self, event):
        message = event["message"]

        await self.send(text_data=json.dumps({"message": message}))

我的应用routing.py

from django.urls import re_path

from . import consumers

websocket_urlpatterns = [
    re_path(r"", consumers.ChatConsumer.as_asgi()),
]
vmdwslir

vmdwslir1#

如果您仍使用channels版本3,请选择pip install 'asgiref==3.3.4',或者升级到channels版本4(该问题在4.0.0中已修复)。
asgiref 3.3.4升级到3.4.1后,我注意到了类似的RuntimeError: Single thread executor already being used, would deadlock
这似乎是由于channels 3.0.4中一个当前未解析的issue,Django开发服务器上的并发请求间歇性失败。
在Python 3.4.0中,asgiref引入了额外的死锁检查。这是一件好事,但可能暴露了通道中的一个潜在问题。从changelog

* Calling sync_to_async directly from inside itself (which causes a deadlock
  when in the default, thread-sensitive mode) now has deadlock detection.

如果您确实需要安装asgiref〉= 3.4.1,开发中的一个解决方法是使用python manage.py runserver --noasgi

相关问题