当分组发送()在中的循环中调用消费者.py

2024-04-29 03:26:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经建立了一个Django应用程序来运行自动测试。我收集用户的输入,关于需要运行的所有测试,以及当用户单击submit按钮时,测试运行几个小时到几天(取决于所选测试的数量),一旦所有测试完成,结果就会显示在模板上。在

现在的问题是,在所有的测试完成之前,用户并没有更新进度。因此,我决定使用Django Channles在我获得单个测试结果时向客户机提供实时更新。在

在我的实现中,我在for循环中调用Group('user-1').send({'text':I})方法。但是我在模板中看到的只是最后一个Group('user-1').send({'text':I})操作的输出。在

根据我的要求,一旦调用Group('user-1').send({'text':i}),则套接字.onmessage模板部分中的()函数应接收消息,并应再次等待后续组(“user-1”)发送的消息。send({'text':i})调用。在

我看到了一个非常相似的问题here,这个问题已经得到了回答,但是这个解决方案对我来说不起作用。请帮助我确定代码中需要更正的内容,如下所示。在

consumers.py
------------
@channel_session_user_from_http
def ws_connect(message, **kwargs):
    http_user = message.user
    if not http_user.is_anonymous:
        message.reply_channel.send({'accept': True})
        Group('user-'+str(http_user.id)).add(message.reply_channel)

def ws_message(message):
    for i in my_results_list:
        Group('user-1').send({'text': i})
        sleep(1)

results.html
------------

 <script>
    // Note that the path doesn't matter for routing; any WebSocket
    // connection gets bumped over to WebSocket consumers

    socket = new WebSocket("ws://" + window.location.host + "/task/summary/");

    socket.onopen = function() {
        socket.send("Hello");
    }

    socket.onmessage = function(message) {
        document.getElementById("demo").innerHTML = message.data;
    }

    socket.onmessage()

    // Call onopen directly if socket is already open
    // if (socket.readyState == WebSocket.OPEN) socket.onopen();
</script>

Tags: text用户send模板httpmessageforif
1条回答
网友
1楼 · 发布于 2024-04-29 03:26:39

您正在调用document.getElementById("demo").innerHTML = message.data;,这会在每次调用demo时覆盖它的内容。你想要这样的东西:

document.getElementById("demo").innerHTML += message.datadocument.getElementById("demo").innerHTML += '<br />' + message.data

如果这个答案解决了你的问题,请把它标记正确。在

相关问题 更多 >