如何使用 django 通道实时流式传输脚本输出?

Fra*_*093 10 python django channel celery

我正在使用 django 来运行 python 脚本。理想情况下,我想将 shell 输出重定向到网页,并且我正在使用 django-channels 来实现此目的。

我试图调用一个异步运行脚本的视图(使用 Celery),然后将 HttpResponse 返回到 html 页面。在此 html 页面上,JavaScript 代码打开一个 websocket 并等待来自脚本任务的消息。

目前脚本在后台正常运行,并且 websocket 连接已建立。但我在网页上看不到输出。

视图.py:

def main_view(request):
    res = script_task.delay()
    client = redis.Redis(host="localhost", port=6379, db=0)
    return render(request, 'run_channels.html')
Run Code Online (Sandbox Code Playgroud)

任务.py:

@shared_task
def macch_main_chan():
    layer = get_channel_layer()
    async_to_sync(layer.send)('out', {'message': 'test'})
    print('Done!')
    return HttpResponse()
Run Code Online (Sandbox Code Playgroud)

路由.py:

from . import consumers
from django.urls import path

websocket_urlpatterns = [
    path('ws/outputmacch/', consumers.PushNotification, name='out'),
]
Run Code Online (Sandbox Code Playgroud)

消费者.py:

class PushNotification(WebsocketConsumer):

    http_user = True

    def connect(self, **kwargs):
        self.accept()

    def receive(self, text, **kwargs):

         async_to_sync(self.channel_layer.send)(
            "out",
            {
                "text": text,
            },
        )

    def disconnect(self, message, **kwargs):
        pass
Run Code Online (Sandbox Code Playgroud)

run_channels.html:

<html>
<head>
    <meta charset="utf-8"/>
    <title>Run</title>
</head>
<body>
    <textarea id="log" cols="100" rows="20"></textarea>
</body>
<script>

    var chatSocket = new WebSocket(
        'ws://' + window.location.host +
        '/ws/outputmacch/');

    chatSocket.onmessage = function(e) {
        console.log(e)
        var data = JSON.parse(e.data);
        var message = data['message'];
        document.querySelector('#log').value += (message + '\n');
    };

    chatSocket.onclose = function(e) {
        console.error('Chat socket closed unexpectedly');
    };
</script>
</html>
Run Code Online (Sandbox Code Playgroud)

目前这是我得到的错误:

kombu.exceptions.EncodeError: Object of type HttpResponse is not JSON serializable
Run Code Online (Sandbox Code Playgroud)

这是否与通道层不可序列化并且不能在 celery 任务中使用这一事实有关?

无论如何,有人对如何在 html 网页上实现实时脚本日志记录有建议吗(最好使用这个工具)?