如果我有以下代码:
$('#do-updates').click(function() {
$.post('/do_updates/',
function(response){
location.reload();
});
});
它调用的python函数是:
def do_update():
for item in items:
do something
我调用的 python 函数大约需要 2m 才能完成。
如果用户执行以下操作:
1 - Click on the #do-updates button
2 - Navigate away from the page after 10s
会发生什么?整个 do_updates()
函数是否会运行,因为它是异步调用的?或者该功能会超时吗?如果确实超时,有什么更好的方法可以在后台执行 do_updates()
函数以使其完成?
最佳答案
如果您不需要在前端执行 do_something 的结果,您可以使用一些异步任务解决方案。 Celery可能是最流行和最容易与 django 一起使用的。 它通过消息队列(rabbitmq、redis、..)将工作委托(delegate)给作为不同进程运行的专门工作人员。
关于javascript - 对 Python 的 Ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19623064/