如何在IIS下的Perl CGI中将进程完全后台化 [英] How to completely background a process in Perl CGI under IIS

查看:86
本文介绍了如何在IIS下的Perl CGI中将进程完全后台化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢这里的其他一些好帖子(例如 this ),我有几种可靠的方法来在Perl脚本中后台处理进程.

Thanks to some other great posts here (like this and that), I have several reliable methods to background a process in a Perl script.

但是,在我的环境(Windows Server 2003中为IIS6)中,它似乎并不完全是背景.使用以下脚本:

However, in my environment (IIS6 on Windows Server 2003), it doesn't seem to completely background. Take this script:

use CGI::Pretty qw(:standard);
print header;
print start_html;
print "hi<br>";
system(1,qw(sleep 10));
print "bye";
print end_html;

运行睡眠过程时,浏览器将保持连接"状态10秒钟.屏幕上会同时显示"hi"和"bye",但浏览器似乎正在等待服务器发送更多信息(Firefox 4和IE6的进度条在那10秒钟内一直缓慢移动).

The browser will stay "connected" for 10 seconds while the sleep process runs. "hi" and "bye" will both be printed on the screen, but the browser seems to be waiting for something more from the server (Firefox 4's and IE6's progress bars keep moving slowly for those 10 seconds).

我希望用户(对于我的真实应用程序)相信该过程是完全扎根的,并且能够完全满意地关闭该网页.在进度栏仍在移动的情况下,我确定他们会坐下来等待其停止,然后再关闭或导航.

I'd like the user (for my real app) to believe the process is fully back-grounded and be fully comfortable being able to close the webpage. With the progress bar still moving, I'm sure they'll sit and wait for it to stop before they close it or navigate away.

请明确说明-该过程显示为背景.我可以关闭网络浏览器.真正的bacgrouind作业可以继续运行(有时长达一个小时),没有任何问题.我只是想从UI角度进行清理.

Just to be clear - the process appears backgrounded. I can close the web browser. The real bacgrouind job continues to run (sometimes for up to an hour) with no issues. I'd just like to clean this up from the UI perspective.

谢谢!

推荐答案

您的问题是CGI程序已完成,但尚未告知Web服务器已完成,并且正在等待该操作,然后才对浏览器实际说它完成了.您将需要弄清楚如何进行通信,或者提出解决该错误的方法.这是一些可能的策略.

Your problem is that the CGI program is done, but the webserver hasn't been told it is done, and is waiting for that before actually saying to the browser that it is done. You'll need to figure out how to communicate that, or else come up with a hack to work around the bug. Here are a few possible strategies.

  1. 在Unix中,告诉Web服务器该消失的正确方法是 daemonize 后台进程.如何执行此操作的详细信息是特定于Unix的.但是,对Win32 perl daemon进行谷歌搜索会导致我进入 Win32 :: Daemon ,然后是 Perlmonks 并在那里询问.那里的Perl专业知识比这里还多.

  1. In Unix the right way to tell the webserver that it is time to go away is to daemonize the background process. The details of how to do this are pretty Unix specific. However Googling for Win32 perl daemon lead me to Win32::Daemon and then Win32::Service, which may help you do something similar for Windows. I've seen comments without details suggesting that this will work. If you need help figuring this out, I would suggest going to Perlmonks and asking there. There is more Perl specific expertise there than here.

假设您有某种可用的批处理作业调度程序.然后,您可以仅安排要完成的任务,而不必启动工作.任务被拾取并运行.如果您需要运行的工作强度足够大,以至于您希望它在网络服务器之外的其他地方运行,则此策略特别有用.

Suppose that you have some sort of batch job scheduler available to you. Then instead of launching a job, you can just schedule a task to be done. The task gets picked up and run. This strategy is particularly useful if the job that you need to run is intensive enough that you'd like it to run somewhere other than your webserver.

您的CGI脚本可以转过来并向Web服务器发出一个http请求,以启动该作业,而无需等待.这是一个简短的示例,向您展示如何做到这一点:

Your CGI script can turn around and issue an http request to the webserver for the job to start, which it doesn't wait for. Here is a brief example showing you how to do that:

#! /usr/bin/perl -w
use strict;
use Carp qw(croak);
use Data::Dumper qw(Dumper);
use LWP::UserAgent;

my $ua = LWP::UserAgent->new();
$ua->add_handler(response_header => sub {croak "complete"}, m_code => 200);
my $response = $ua->get("http://www.perl.com/");

if ($response->is_success) {
    print "Request backgrounded\n";
}
else {
    print "Uh, oh.\n";
}

这篇关于如何在IIS下的Perl CGI中将进程完全后台化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆