客户端浏览器关闭时服务器端 Perl CGI 脚本中断

发布于 2024-09-14 01:28:33 字数 1450 浏览 4 评论 0原文

我已经尝试解决一个小问题很长一段时间了,但似乎我无法解决。

我编写了一个 HTML 页面,它在提交表单时调用 perl CGI 脚本。该 CGI 在服务器端执行一定数量的任务,我让脚本将这些任务的步骤打印到 HTML 页面上的 iframe 中。问题是,如果客户端关闭浏览器或只是退出页面,CGI 脚本就会在服务器端中断

HTML 代码:

<form action="/path/to/script.cgi" method="post" enctype="multipart/form-data" target="processing">
 <p>File to analyse: <input type="file" name="filename" size="50"/></p>
 <p><input type="submit" name="Submit" value="Analyse" /></p>
</form></font></p>

<p style="margin-left: 20"><font face="Arial" size="2" color="#000000">Analysing the file may take a while</font></p>
<iframe name="processing" width="70%" height="300">

</iframe>

CGI 脚本:

my $query = new CGI;
print $query->header(-type => 'text/plain');
function1();
function2_takesLongTime($parameter);
function3();

可以说“function1”只是为文件分析做一些准备(创建文件夹等)。 “function2”是大函数(可能持续 15 分钟)。它是一个 Perl 程序,并且有很多“打印”,由于 CGI 的“text/plain”标头,这些“打印”被重定向到 html 页面的 iframe(在程序中不使用缓冲 $|)。 httpd 的配置使超时远大于 15 分钟,因此它不是来自那里。 “function3”是清理。

如果客户端停留在 html 页面上,则 CGI 脚本可以完美运行。 如果客户端停止(例如用户关闭窗口),函数 1 和 2 将在服务器端执行,但此后脚本似乎会被中断,因为没有进行任何清理。

我尝试使用系统命令将“function2”作为独立程序启动,或者创建一个perl库并调用该库的主函数,它仍然以相同的方式结束。 我认为无论客户端是否停留在页面上,服务器端脚本仍然会一直运行。是否是因为CGI脚本的“text/plain”头无法返回给客户端而导致脚本中断?

如果有人能帮助我解决这个问题,我将非常感激。

I've been trying to solve a little problem for quite a while now but it seems I'm not able to.

I wrote an HTML page which calls a perl CGI script upon submitting a form. This CGI executes a certain number of tasks server-side and I made the script print the steps of these tasks into an iframe on the HTML page. The thing is, if the client closes his browser or just goes out of the page, the CGI script is interrupted on server-side.

HTML code:

<form action="/path/to/script.cgi" method="post" enctype="multipart/form-data" target="processing">
 <p>File to analyse: <input type="file" name="filename" size="50"/></p>
 <p><input type="submit" name="Submit" value="Analyse" /></p>
</form></font></p>

<p style="margin-left: 20"><font face="Arial" size="2" color="#000000">Analysing the file may take a while</font></p>
<iframe name="processing" width="70%" height="300">

</iframe>

CGI script:

my $query = new CGI;
print $query->header(-type => 'text/plain');
function1();
function2_takesLongTime($parameter);
function3();

So lets say that "function1" is just preparing some things for the file analysis (creating folders etc.).
"function2" is the big function (may last 15 minutes). It is a perl program and it has a lot of "prints" which, because of the "text/plain" header of the CGI, are redirected into the iframe of the html page (using no buffering $| in the program). httpd is configured so that the time-out is much longer than 15 minutes so it doesn't come from there.
"function3" is clean-up.

If the client stays on the html page, the CGI script runs perfectly.
if the client is stopped (e.g. the user closes the window), function 1 and 2 are executed server-side, but the script seems to be interrupted after that because no clean-up is made.

I tried launching "function2" as an independent program with a system command, or creating a perl library and calling main function of this library, it still ends up the same way.
I thought that no matter if the client stays or not on the page, the server-side script would still run all the way through. Is it because of the "text/plain" header of the CGI script which can't be returned to the client that the script is interrupted?

If anyone can help me out with this, I'd be very grateful.

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

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

发布评论

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

评论(2

瀞厅☆埖开 2024-09-21 01:28:33

您需要以不同的方式构建您的软件。使用某种异步编程,作业队列就可以了,而使用 CGI 程序仅将作业放入队列中。使用您已经提到的库进行转换。

你似乎对这个概念不太熟悉,所以我推荐一个非常简单的:Qudo 。如果您发现它限制太多,请切换到 Gearman,我认为这是最受欢迎的一个在 Perl 世界中(请参阅接口 Gearman::XSGearman)。

TIMTOWTDI:如果您可以完全放弃 CGI.pm 提供的执行环境,请参见 Catalyst 的 <代码>RunAfterRequest

You need to architect your software differently. Use some sort of asynchronous programming, a job queue would do fine, and use the CGI program only to put a job in the queue. Make the transition by using the library you have already mentioned.

You don't seem to be familiar with the concept, so I recommend a very simple one: Qudo. In case you find it too restrictive, switch to Gearman, which I think is the most popular one in the Perl world (see interfaces Gearman::XS and Gearman).

TIMTOWTDI: If you can give up on the execution environment provided by CGI.pm altogether, see e.g. Catalyst's RunAfterRequest.

无所谓啦 2024-09-21 01:28:33

通过 CGI 观看长进程提供了解决此问题的直接方法。

Watching long processes through CGI provides a straightforward way to solve this issue.

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