如果我正确理解了您的问题,您想使用 Django Channels 而不在生产中使用任何 Django?
这不是问题,但您仍然必须在您的 virtualenv 中安装 Django。我假设,您知道如何配置应用程序的 Channels 部分,我将仅描述您如何使其工作。
- 您仍然需要使用 django 启动项目:
django-admin startproject 可以解决问题
- 现在您有一个工作的“核心”项目,您还需要设置您的频道
routing。我在核心项目文件夹中使用了routing.py(在settings.py 旁边)。您还需要您的消费者,通常在同一位置的 consumers.py。
- 然后,您需要在
settings.py 中设置您的频道部分。例如:
# Channels
ASGI_APPLICATION = 'myproject.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
},
},
}
那么您需要编辑 INSTALLED APPS SECTION:
INSTALLED_APPS = [
#if you dont want any of "standard" django just comment it out
#'django.contrib.admin',
#'django.contrib.auth',
#'django.contrib.contenttypes',
#'django.contrib.sessions',
#'django.contrib.messages',
#'django.contrib.staticfiles',
'channels', # <--- here you enable the channels part
]
- 你准备好了!我不知道您使用的是什么 ASGI 服务器,但您将其指向您的 routing.py 文件中的应用程序对象。例如:
from channels.routing import ProtocolTypeRouter,URLRouter,ChannelNameRouter
from django.urls import path
from . import consumers # your actual consumers for the endpoint of routes
application = ProtocolTypeRouter({
# Empty for now (http->django views is added by default)
"websocket": URLRouter(
[
path('ws/route1/', consumers.ConsumerA),
path('ws/route2/', consumers.ConsumerB),
# and so on...
]
),
"channel": ChannelNameRouter({
"channel_name": consumers.ChannelConsumer,
# and other channels you may have...
})
})
基本上就是这样。所以一个 TLDR,你仍然需要运行 django,但是你可以在设置中去除除通道之外的所有内容......