显示 PYTHON CGI 脚本的进度

发布于 2024-12-22 15:41:26 字数 310 浏览 3 评论 0原文

我有一个 PYTHON CGI 脚本(内容类型:text/plain),执行大约需要 10 分钟。 我想在浏览器上查看脚本的执行状态。

就像下面这样:

执行脚本的第 1 部分...

执行脚本的第 2 部分..

执行脚本的第 3 部分..

执行完成

我正在使用打印语句,但只有在脚本完成执行后才会一起输出所有打印语句,并且不是一一的。

请帮忙..

I have a PYTHON CGI script(Content-type: text/plain) which takes about 10 minutes to execute.
I want to see the execution status of my script on my browser .

Like below:

Part 1 of script executed...

Part 2 of script executed..

Part 3 of script executed..

Execution Complete

I am using print statements, but it is outputting all the print statements all-together only after the script has completed execution, and not one by one.

Please help ..

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

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

发布评论

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

评论(3

何其悲哀 2024-12-29 15:41:26

我怀疑真正的阻塞点是网关(Web 服务器),而不是您的 CGI 应用程序。从技术上讲,网关必须验证响应并确保其符合网关与客户端一起使用的 HTTP 版本。

我不确定在处理所有请求之前是否允许网关转发标头。如果您查看第 3.1 节“服务器职责”下的 CGI 规范,您可以阅读下列的:

服务器必须对本规范要求的客户端请求数据执行翻译和协议转换。此外,即使 CGI 脚本不符合此规范,服务器仍对客户端保留遵守相关网络协议的责任。

如果脚本需要很长时间才能运行并且您需要定期更新,那么最好重新考虑您的架构。看一下这种方法的更经典策略,主要是让脚本在后台进程中运行,该进程写入数据库并编写一些 AJAX 代码以从服务器提取通知。根据您用作服务器堆栈的内容,您也许还可以编写通过 进行通信的应用程序Web 套接字 允许您保持连续连接并在需要时发送更新。

I suspect the real blocking point is the gateway (web server), not your CGI application. The gateway technically has to validate the response and make sure it conforms to the HTTP version the gateway is using with the client.

I'm not sure the gateway is even allowed to forward the headers until all of the request is processed. If you take a look at the CGI specification under section 3.1 "Server Responsibilities", you can read the following:

The server MUST perform translations and protocol conversions on the client request data required by this specification. Furthermore, the server retains its responsibility to the client to conform to the relevant network protocol even if the CGI script fails to conform to this specification.

If the script takes a long time to run and you want periodic updates, you are much better off re-thinking your architecture. Take a look at more classic strategies for this approach, mainly having the script run in a background process which writes to a database and have an write some AJAX code to pull notifications from the server. Depending on what you're using as a server stack, you might also be able to write your application for communication over a web socket which would allow you to hold a continuous connection and send updates whenever you want.

瞄了个咪的 2024-12-29 15:41:26

尝试这样做:

import sys
...

print "Part 1 of script executed..."
sys.stdout.flush() # do this after the print

这会刷新标准输出缓冲区,这可能是导致您的问题之一的原因。您的浏览器或网络服务器也很可能导致此行为,在这种情况下解决此问题将更加困难。


我不确定您在这里想做什么,但您可能想使用 AJAX 异步触发脚本,然后异步加载数据。对于浏览器和网络服务器来说,这比花费几分钟的 CGI 脚本更容易处理。

Try doing:

import sys
...

print "Part 1 of script executed..."
sys.stdout.flush() # do this after the print

This flushes the standard out buffer, which could be the cause of one of your issues. It is also very likely that your browser or webserver might be causing this behavior, in which case solving this problem will be more difficult.


I'm not sure what you are trying to do here, but you may want to use AJAX to asynchronously fire off your script and then asynchronously load the data back. This is better behavior for browsers and webservers to handle than a cgi script taking several minutes.

╭ゆ眷念 2024-12-29 15:41:26

尝试逐部分产生输出,而不是返回完整响应。您使用任何网络框架吗? Web.py 有一个很好的例子。正如他们所说,

[...] 您需要确保添加 Transfer-Encoding 分块标头才能正确显示。否则浏览器将在向您显示之前缓冲所有数据。

如果您没有使用 web.py 或其他 Web 框架,您可以尝试一下——不过我不知道它是否有效。

Try yielding output part by part instead of returning a full response. Are you using any web framework? Web.py has a good example. As they say,

[...] you need to make sure you add the Transfer-Encoding chunked header for it to display properly. Otherwise the browser will buffer all data before displaying it to you.

If you're not using web.py or another web framework, you may try it anyway -- I don't know if it will work though.

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