本文介绍了Django Channels 2-我需要开始工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了在我的代码中某处使用channel_layer.send(....

I've read where I only need to start a worker if somewhere in my code I use channel_layer.send(....

这是正确的吗?我以为该工作人员是处理所有websocket请求的人.

Is this correct? I thought the worker was the one handling all websocket requests.

请赐教.谢谢.

推荐答案

问题背后有许多有趣的概念需要澄清.首先让我们开始使用WebSocket.

There are many interesting concepts behind your question which need clarification.Let's start with WebSocket usage first.

您最好是使用Django Channels的WebSocket,因为Django仅管理HTTP.

WebSocket might be very well you're primary interest in using Django Channels, since Django alone just manages HTTP.

为此,您不需要通道层,也不需要正在运行的工作程序.

开发中,通常是启动标准Django项目时看到的内容:

In development, this is what you normally see starting a standard Django project:

$ python manage.py runserver

Performing system checks...
...
Django version 2.1.5, using settings 'djlistener.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

但是,按照Django Channels文档的建议,将 channels 添加到INSTALLED_APPS并定义 ASGI_APPLICATION 设置,您会注意到输出略有不同:

However, adding channels to INSTALLED_APPS and defining the ASGI_APPLICATION setting as suggested by the Django Channels documentation, you'll notice a slightly different output:

$ python manage.py runserver

Performing system checks...
...
Django version 2.1.5, using settings 'djlistener.settings'
Starting ASGI/Channels version 2.1.6 development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

这是因为Channels开发服务器已经从Django开发服务器上接管了,您可以立即使用HTTP和WebSocket通信开始.

That's because the Channels development server has taken over from the Django development server, and you can start immediately using both HTTP and WebSocket communications.

生产中:

  • 您可以选择将Daphne用于HTTP和WebSockets请求,因为Daphne将在HTTP和WebSocket之间自动协商

  • you can choose to use Daphne for both HTTP and WebSockets requests, since Daphne will auto-negotiates between HTTP and WebSocket

,或者,您可以拆分HTTP和WebSocket流量:通过WSGI服务器运行标准HTTP请求,仅将达芙妮(或uvicorn)用于WSGI无法完成的事情,例如WebSocket,HTTP长轮询或其他IoT协议

or, you can split HTTP and WebSocket traffic: running standard HTTP requests through a WSGI server, and using Daphne (or uvicorn) only for things WSGI cannot do, like WebSockets, HTTP long-polling or other IoT protocols

参考文献:

https://channels.readthedocs.io/en/Latest/deploying.html#http-and-websocket

https://avilpage.com/2018/05/deploying-scale-django-channels.html

从2.0版开始,频道层是频道的完全可选部分.

Channel Layers are an entirely optional part of Channels as of version 2.0.

它们提供了一种抽象,使您可以出于多种目的在应用程序的不同实例之间进行交谈.

They provide an abstraction which allows you to talk between different instances of an application for several purposes.

如何?当您发送消息时,正在收听另一端的组或频道的消费者会收到该消息.

How ? When you send a message, it is received by the consumers listening to the group or channel on the other end.

我使用Channel Layers的主要动机是组概念,这让我向注册到特定组的所有WebSocket客户端广播消息(通过连接).为此,我不需要工作人员:我使用channel_layer.group_send()将消息发送到组,然后Channels Layer将其发送给Consumers,然后由Consumers将其发送给WebSocket客户端.

My primary motivation for using Channel Layers is the Group concept, which let's me broadcast a message to all WebSocket clients registered (upon connection) to a specific group. For that purpose, I don't need a worker: I send my message to the group with channel_layer.group_send(), and Channels Layer delivers it to the Consumers, which in turn send it to the WebSocket client.

通道层的一种非常特殊的用法是将一些工作委派给一组侦听固定通道名称的工作服务器,作为一个简单的任务队列.

A very specific use of Channel Layers is that of delegating some work to a set of worker servers listening on fixed channel names, as a simple task queue.

在这里(并且仅在此处)是"runworker"命令的出现位置.

Here (and only here) is where the "runworker" command comes in.

通常,您会为此使用其他众所周知的解决方案,例如Celery或Django-rq.但是在某些情况下,对于简单的后台任务队列,这可能是一种廉价且非常低延迟的选择.

Most often you'll use other well known solutions like Celery or Django-rq for this; but on some occasions, this might be a cheap and very-low-latency alternative for a simple background tasks queue.

参考文献:

https://channels.readthedocs.io/zh-CN/latest/topics/worker.html#worker-and-background-tasks

这篇关于Django Channels 2-我需要开始工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:34