从 CGI 在后台运行 python 脚本

发布于 2024-08-25 21:10:41 字数 211 浏览 2 评论 0原文

我有一个 python CGI,它在后台运行一些脚本并在 html 页面中显示标准输出。当用户单击页面中的某个按钮时,我运行脚本。

我的问题是,当脚本开始运行时,页面会变得繁忙,并且用户无法使用页面中的其他客户端功能。

我想要的是: 当用户单击按钮时,脚本应在后台运行,并应在运行完成时通知 CGI。然后 CGI 显示应该运行脚本的标准输出。

这怎么能做到呢?

I have a python CGI which runs some script in the background and shows the stdout in the html page. I run the script when the user clicks some button in the page.

My problem is when the script starts running the page becomes busy and the user can't use the other client side features in the page.

What I want is:
The script should run in background when the user clicks the button and should notify the CGI when run is complete. Then the CGI show should the stdout of the script run.

How can this be done?

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

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

发布评论

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

评论(1

瑾兮 2024-09-01 21:10:41

好吧,简短的回答:你不能。

中等答案:CGI 很糟糕。

长答案:CGI 的工作原理是运行脚本并将脚本打印的任何内容返回到浏览器。如果您的脚本仍在运行,浏览器将等待。如果您的脚本启动后台作业并将数据返回到浏览器,则后台作业无法通知 CGI 脚本,因为它已经完成。

您必须选择替代解决方案。

将后台作业的结果保存到文件、数据库或其他持久存储中,并使用页面中的另一个链接让用户请求该数据,该链接运行不同的代码来检索保存的结果并显示它们。

另一种方法是在浏览器中使用AJAX技术。编写javascript代码在后台对数据进行请求。因此,在脚本运行时,浏览器仍然可以响应其他页面元素。

well, short answer: you can't.

medium answer: CGI sucks.

long answer: CGI works by running your script and returning whatever your script prints to the browser. If your script is still running, the browser will be waiting. If your script launches a background job and returns data to the browser, then the background job can't notify the CGI script because it is already done.

You must choose an alternate solution.

Save the results of the background job to a file, database, or some other persistent storage, and make the user request that data, using another link in your page, which runs a different code that retrieves the saved results and display them.

Another way is to use AJAX techniques in the browser. Write javascript code to do the request to the data in the background. So the browser can still be responsive with other page elements while the script is running.

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