简体   繁体   English

如何在Perl CGI脚本中产生长时间运行的进程?

[英]How can I spawn a long running process in a Perl CGI script?

I'm writing a Perl CGI script right now but it's becoming a resource hog and it keeps getting killed by my web host because I keep hitting my process memory limit. 我现在正在编写一个Perl CGI脚本,但是它正在成为一种资源消耗,并且由于我不断达到进程内存限制而一直被我的Web主机杀死。 I was wondering if there is a way I can split the script I have into multiple scripts and then have the first script call the next script then exit so the entire script isn't in memory at once. 我想知道是否有一种方法可以将我拥有的脚本拆分为多个脚本,然后让第一个脚本调用下一个脚本,然后退出,以便整个脚本不会立即存储在内存中。 I saw there is an exporter module but I don't know how to use it yet as I'm just learning Perl, and I don't think that will solve my memory problem but I might be wrong. 我看到有一个导出器模块,但是我在学习Perl时还不知道如何使用它,并且我认为这不会解决我的内存问题,但是我可能错了。

See Watching long processes through CGI . 请参阅通过CGI监视长过程

On the other hand, just managing memory better might also solve your problem. 另一方面,仅更好地管理内存也可以解决您的问题。 For example, if you are reading entire files into memory at once, try to write the script so that it handles data line-by-line or in fixed sized chunks. 例如,如果您要一次将整个文件读取到内存中,请尝试编写脚本,以使其逐行或以固定大小的块处理数据。 Declare your variables in the smallest possible scope. 在尽可能小的范围内声明变量。

Try to identify what part of your script is creating the largest memory footprint and post the relevant excerpt in a separate question for more memory management suggestions. 尝试确定脚本的哪个部分正在创建最大的内存占用,并在另一个问题中张贴相关摘录,以获取更多内存管理建议。

If applicable, make the computation/generation off line. 如果适用,使计算/生成脱机。

create a daemon or a scheduled job that creates a static version of the results, the daemon can create a new version of the results on events (eg files modified) or in set intervals. 创建守护程序或创建结果静态版本的计划作业,守护程序可以根据事件(例如,已修改的文件)或按设置的时间间隔创建结果的新版本。

If you generate the page depending on client input, look into separating the logic so it's possible to cache at least parts of the application. 如果您根据客户端输入生成页面,请考虑分离逻辑,以便可以缓存至少一部分应用程序。

Side note, unless it suites your needs, I'd move away from CGI altogether and look into mod_perl or fastcgi , where you have persistent perl processes to handle requests which saves the overhead of forking a new perl interpretor, loading modules and etc. 旁注,除非它满足您的需求,否则我将完全放弃CGI,而转而研究mod_perlfastcgi ,那里有持久的perl进程来处理请求,从而节省了派生新的perl解释器,加载模块等的开销。

Yes, you can start another perl-script from a perl-script and then exit the calling script: 是的,您可以从一个perl脚本中启动另一个perl脚本,然后退出调用脚本:

http://perldoc.perl.org/functions/fork.html http://perldoc.perl.org/functions/fork.html

Example Code: 示例代码:

#!/usr/bin/perl

my $pid = fork();
if (not defined $pid) {
    print "resources not avilable.\n";
} elsif ($pid == 0) {
    print "IM THE CHILD\n";
    sleep 5;
    print "IM THE CHILD2\n";
    exit(0);
} else {
    print "IM THE PARENT\n";
    waitpid($pid, 0);
}
print "HIYA\n";

But this won't work, if you want the second script being able to use the CGI to communicate with your webserver/user. 但是,如果您希望第二个脚本能够使用CGI与您的Web服务器/用户进行通信,则此方法将无效。 If you are running the perl-script as CGI, then it has to return the result to the user. 如果您将perl脚本作为CGI运行,则必须将结果返回给用户。

So you have two ways of dealing with this problem: 因此,您有两种方法可以解决此问题:

  • Try to find out, why you are using so much memory and improve the script. 尝试找出原因,为什么要使用这么多的内存并改进脚本。

  • If there is really no way to reduce memory-consumption, you can use the daemonized perl-script as worker-process, that do the calculations and returns the results to your CGI-perl-script, which has to wait for the result before termination. 如果确实没有减少内存消耗的方法,则可以将守护程序的perl-script用作工作进程,进行计算并将结果返回到CGI-perl-script,后者必须在终止之前等待结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM