Django 视图中长时间、缓慢的操作会导致超时。有什么办法可以让 Python 代替 AJAX 说话吗?

发布于 2024-08-08 13:15:29 字数 271 浏览 1 评论 0原文

我从事 Python 编程已经有一段时间了,但 DJango 和 Web 编程对我来说总体来说还是陌生的。

我在 Python 视图中执行了一个很长的操作。由于我认为 local() 函数需要很长时间才能返回,因此存在 HTTP 超时。公平地说,我理解那部分。

立即向用户返回 HTTP 响应,然后动态显示页面中某些 Python 代码的结果的最佳方法是什么?我怀疑答案可能在于 AJAX,但我不确定如何从服务器上的 Python 提供客户端上的 AJAX,甚至不知道通常用来执行此类操作的模块。

I've been programming Python a while, but DJango and web programming in general is new to me.

I have a very long operation performed in a Python view. Since the local() function in my view takes so long to return, there's an HTTP timeout. Fair enough, I understand that part.

What's the best way to give an HTTPresponse back to my users immediately, then dynamically show the results of some python code within the page? I suspect the answer may lie in AJAX but I;m not sure how AJAX on the client can be fed from Python on the server, or even the modules one would commonly use to do such a thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁我热情 2024-08-15 13:15:29

Ajax 不需要服务器端任何特定的技术。您所需要的只是以客户端的某些 Javascript 可以理解的某种形式返回响应。 JSON 在这里是一个很好的选择,因为它很容易在 Python 中创建(2.6 中有一个 json 库,Django 有用于其他版本的 django.utils.simplejson 库)。

因此,您需要做的就是将数据放入 JSON 形式,然后像发送任何其他响应一样发送它 - 即通过将其包装在 HTTPResponse 中。

Ajax doesn't require any particular technology on the server side. All you need is to return a response in some form that some Javascript on the client side can understand. JSON is an excellent choice here, as it's easy to create in Python (there's a json library in 2.6, and Django has django.utils.simplejson for other versions).

So all you need to do is to put your data in JSON form then send it just as you would any other response - ie by wrapping it in an HTTPResponse.

伪装你 2024-08-15 13:15:29

一种方法是使用 AJAX/JS 或正常方式提交任务,在视图的后台启动它并立即返回。然后在客户端使用AJAX/JS定期检查任务是否完成。如果完成,请重新加载页面或向客户端提供链接。

客户端“请使用此数据启动任务。”->服务器

客户端<-“任务开始!”服务器

客户“完成了吗?”->服务器

客户端<-“不。”服务器

客户“完成了吗?”->服务器

客户端<-“是的,这是一个可以查看结果的链接”服务器

在无需客户端请求的情况下从服务器向客户端发送数据是可能的,很好,(该技术称为 Comet)在你的情况下并不是真的有必要。

One way is to submit the task using AJAX/JS or the normal way, start it in background in your view and return immediately. Then use AJAX/JS on client side to periodically check if task is done. If it's done reload the page or provide a link to the client.

CLIENT "Please start a task using this data."-> SERVER

CLIENT <- "Task started!" SERVER

CLIENT "Done?"-> SERVER

CLIENT <- "Nope." SERVER

CLIENT "Done?"-> SERVER

CLIENT <- "Yep, here's a link where you can view results" SERVER

While sending data from server to client without client asking for it is possible, well kind a, (the technology is called Comet) it isn't really necessary in your case.

羁绊已千年 2024-08-15 13:15:29

我不确定这是否是您正在寻找的,但是 也许这个问题(How to Implement a minimary server for AJAX in Python?)很有帮助。在我的回答中,我给出了一个最小的例子(写得不是很好,例如我现在会使用 jquery...)。

编辑:根据OP的要求,这里是一个带有JQuery的前端示例。请注意,我不是这方面的专家,因此可能会出现问题。此示例应该与 JSON-RPC 后端一起使用,例如 这个

<html>
<head>

<title>JSON-RPC test</title>

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="json2.js"></script>

<script type="text/javascript">

function test_button() {
    var data = $("[name=test_text]").val();
    var json_object = {"method": "power",
                       "params": [parseInt(data), 3],
                       "id": "test_button"};
    var json_string = JSON.stringify(json_object);
    $.post("frontend.html", json_string, test_callback, "json")
}

function test_callback(json_object) {
    $("#test_result").text(json_object.result.toString());
}

</script>

</head>
<body>

<input type="text" name="test_text" value="2" size="4">
** 3 =
<span id="test_result">0</span>
<input type=button onClick="test_button();" value="calc" title="calculate value">

</body>
</html>

I'm not sure if this is what you are looking for, but maybe this question (How to implement a minimal server for AJAX in Python?) is helpful. In my answer I give a minimal example (which is not very well written, for example I would now use jquery...).

Edit: As requested by the OP, here is an example for the frontend with JQuery. Note that I'm no expert on this, so there might be issues. This example is supposed to work with a JSON-RPC backend, like this one.

<html>
<head>

<title>JSON-RPC test</title>

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="json2.js"></script>

<script type="text/javascript">

function test_button() {
    var data = $("[name=test_text]").val();
    var json_object = {"method": "power",
                       "params": [parseInt(data), 3],
                       "id": "test_button"};
    var json_string = JSON.stringify(json_object);
    $.post("frontend.html", json_string, test_callback, "json")
}

function test_callback(json_object) {
    $("#test_result").text(json_object.result.toString());
}

</script>

</head>
<body>

<input type="text" name="test_text" value="2" size="4">
** 3 =
<span id="test_result">0</span>
<input type=button onClick="test_button();" value="calc" title="calculate value">

</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文