显示 PYTHON CGI 脚本的进度
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑真正的阻塞点是网关(Web 服务器),而不是您的 CGI 应用程序。从技术上讲,网关必须验证响应并确保其符合网关与客户端一起使用的 HTTP 版本。
我不确定在处理所有请求之前是否允许网关转发标头。如果您查看第 3.1 节“服务器职责”下的 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:
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.
尝试这样做:
这会刷新标准输出缓冲区,这可能是导致您的问题之一的原因。您的浏览器或网络服务器也很可能导致此行为,在这种情况下解决此问题将更加困难。
我不确定您在这里想做什么,但您可能想使用 AJAX 异步触发脚本,然后异步加载数据。对于浏览器和网络服务器来说,这比花费几分钟的 CGI 脚本更容易处理。
Try doing:
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.
尝试逐部分
产生
输出,而不是返回
完整响应。您使用任何网络框架吗? Web.py 有一个很好的例子。正如他们所说,如果您没有使用 web.py 或其他 Web 框架,您可以尝试一下——不过我不知道它是否有效。
Try
yield
ing output part by part instead ofreturn
ing a full response. Are you using any web framework? Web.py has a good example. As they say,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.