如何运行外部进程而不等待 Perl CGI 脚本完成?

发布于 2024-08-29 18:28:48 字数 398 浏览 2 评论 0原文

是否可以继续显示 CGI 脚本的 HTML,而不等待子进程完成,但当 CGI 脚本完成时,子进程应该保持活动状态。

这就是我所拥有的,

--显示 HTML 页面

# html 页面设置...所以标题/其他内容

#-c、-h 只是参数
system("perl subprocess.pm -c params -h 1 &");

#实际打印html页面设置
...

由于一些奇怪的原因,它在输出 html 页面之前等待子进程完成,即使我包含了 Linux 的异步系​​统调用。它不会立即呈现页面。是否可以在不等待子进程完成的情况下打印 html 页面?

Is it possible to continue displaying a CGI script's HTML without waiting for a child process to complete, yet the child process should stay alive when the CGI script is complete.

Here's what I have,

-- Display HTML page

# html page set up... so header/other stuff

#the -c, -h are params are just params
system("perl subprocess.pm -c params -h 1 &");

#actually print the html page setup
...

For some weird reason, it waits for the subprocess to finish before it outputs the html page even though I included the asynchronous system call for linux. It doesn't render the page immediately. Is it possible to print the html page without waiting for the subprocess to finish?

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

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

发布评论

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

评论(3

各自安好 2024-09-05 18:28:48
  1. 这是可能的 - 请确保 autofulush 已打开 ($|=1;) 如此处讨论,或者在“Perl Cookbook”(第 7.12 章。刷新输出)中讨论这一点。

    您还需要通过“守护进程”来解耦子进程 - 查看以太坊对另一个问题的回答作为示例

  2. 但是,这是不可靠的,并且由于 HTTP 超时而容易损坏。因此,在网页上显示连续变化的输出的正确解决方案是:

    a.更简单的解决方案:

    • 您的 CGI 脚本打印到临时 HTML 文件

    • 偶尔它会获取该临时文件,关闭 HTML 并将当前最新版本复制到 htdocs 树中可见的某个临时位置(例如,客户端可以通过 URL 引用 - 例如 http://your_server/tmp/AYZ122.html

    • 在创建临时文件的原始版本并生成子进程后,您的原始 CGI 会生成重定向到 http://your_server/tmp/AYZ122.html 的 HTTP 响应。

    • 该位置的临时 HTML 文件有一个 META REFRESH,指示它每 N 秒(比如 10)刷新一次,并有适当的消息/CSS 警告用户加载正在进行中,请稍候。< /p>

    b.更难的解决方案:

    • 有点类似,主 CGI 脚本创建一个发送给用户的“请稍候”HTML 页面,并启动一个写入临时文件的子进程。

    • 但是,子进程创建的临时文件不一定是整个 HTML 页面,而是一些数据文件。

    • 由原始 CGI 脚本创建的主 HTML 页面包含一个循环,该循环发出 AJAX 调用来检索最新数据(检索过程将读取子进程写入的临时文件)并根据最新数据更新 HTML 页面.

  1. It's possible - please ensure that the autofulush is on ($|=1;) as discussed here, or for that matter in "Perl Cookbook" (Chapter 7.12. Flushing Output).

    You also need to decouple the child process by "daemonizing" it - see Ether's answer to a different question for an example

  2. However, this is not reliable AND is subject to easy breakage due to HTTP timeouts. Therefore the proper solution to display a continuously changing output on a web page is to either:

    a. Simpler solution:

    • Your CGI script prints to a temporary HTML file

    • once in a while it takes that temp file, closes the HTML and copies the current latest version to some temp location visible from htdocs tree (e.g. that can be referenced by a client via a URL - say http://your_server/tmp/AYZ122.html

    • your original CGI's produces an HTTP response redirecting to http://your_server/tmp/AYZ122.html after creating the very original version of the temp file and spawning off the child process.

    • The temp HTML file in that location has a META REFRESH which instructs it to refresh itself every N seconds (say 10) and appropriate message/CSS for the user warning that the loading is in progress and please wait.

    b. Harder solution:

    • Somewhat similar in that the main CGI script creates a "please wait" HTML page sent to the user, and launches a child process which writes a temp file.

    • The temp file created by the child process, however, is not necessarily a whole HTML page, but some data file.

    • The main HTML page created by original CGI script contains a loop which issues AJAX calls that retrieve the latest data (the retrieval process will read the temp file written by child process) and updates the HTML page according to latest data.

糖果控 2024-09-05 18:28:48

还有另一种解决方案:使用SSI(服务器端包括)。
您的 CGI 程序:

  • 1) 创建一个临时网页“请稍候”
    包含指令 #include 虚拟日志文件;
  • 2) 初始化日志文件,fe 在其中写入第一条消息;
  • 3)静默启动主子进程;
  • 4) 静默启动检查器子进程;
  • 5) 生成一个带有 META 标签 REFRESH 的网页
    重定向到临时网页。临时网页有
    元标记刷新而不重定向。

检查器子流程
每 N 秒检查一次主子进程的状态。当
主进程完成,无论正常还是异常,检查器
流程创建最终网页,没有 REFRESH 元标记
与临时网页的名称相同。

效果是日志文件连续地显示给用户
在主子进程完成之前每 N 秒更新一次。

There is another solution: use SSI (Server side includes).
Your CGI-program :

  • 1) creates a temporary web-page "Please wait"
    that contains directive #include virtual log-file;
  • 2) initializes log-file, f.e. writes there the first message;
  • 3) silently launches the main subprocess;
  • 4) silently launches the checker subprocess;
  • 5) generates a web page that has META-tag REFRESH with
    redirection to the temporary web-page. The temporary web page has
    meta-tag REFRESH without redirection.

The checker sub-process
every N seconds checks the status of the main subprocess. When the
main process finishes, either normally or abnormally, the checker
process creates the final web page without REFRESH meta-tag with
the same name as the temporary web-page.

The effects is that the log-file is shown to a user and continuously
updated every N seconds before the main subprocess finishes.

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