csdnceshi62 2010-09-06 09:46 采纳率: 100%
浏览 117

Django-Ajax HttpResponse延迟

I have a django view function that calls another function if a condition is true, the function is called as a separate process, the view returns a status variable which shows if the function was called or not, the status is returned to a ajax click event assigned to a button.
The problem is that when the function 'do_work' is executed ajax success function isn't run until 'do_work' terminates.

view function /update/

if condition:
        p = Process(target=do_work, args(pack,))
        p.daemon = True
        p.start()
        success = "True"
else:
        success = "False"
print success
return HttpResponse(success)

Ajax call

 $('button.update').click(function() {
                id = $(this).attr('id');
                $.post("/update/", {
                pack : id,
                }, function(result){
                    alert(result);
                    });
                return false;
                });

the 'print success' gets executed right away in both cases, but the alert wont pop up until the function do_woks terminates

  • 写回答

1条回答

  • 三生石@ 2010-09-06 10:37
    关注

    Although you see the printed message before you return HttpResponse(success), that return statement in itself can't asynchronously finish the response.

    Presumably after that function returns, your web server is still waiting for something else to happen (such as for all child processes to terminate) before it finishes the response.

    For asynchronous tasks in Django I highly recommend Celery.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决