从 CGI 在后台运行 python 脚本
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,简短的回答:你不能。
中等答案: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.