I'm developing some CGI scripts and I'm trying to find a solution to decrease the "starting time" produced when I import a lot of modules with "use".
我正在开发一些CGI脚本,我正在尝试找到一种解决方案,以减少使用“use”导入大量模块时产生的“开始时间”。
Update:
The solutions provided are nice, but the scripts I'm working runs both in console and CGI mode checking if some typical HTTP environment variables are present.
提供的解决方案很不错,但我正在使用的脚本在控制台和CGI模式下运行,检查是否存在一些典型的HTTP环境变量。
In "console mode" they dump the data "normally", and in "html mode" they make some realtime replacements and send other HTTP headers to the client.
在“控制台模式”中,它们“正常”转储数据,在“html模式”下,它们会进行一些实时替换,并将其他HTTP标头发送到客户端。
I'd like to improve the startup time in both cases.
我想在两种情况下改善启动时间。
6 个解决方案
#1
Consider using CGI::Fast in order to start one perl process to handle multiple requests. It took me very little effort to change some of my big CGI scripts over to CGI::Fast. Unlike mod_perl, it's very easy to run CGI::Fast on hosting sites because you can restart your scripts without restarting Apache (at least that's what my hoster told me when I asked for mod_perl).
考虑使用CGI :: Fast来启动一个perl进程来处理多个请求。我花了很少的精力将一些大的CGI脚本改为CGI :: Fast。与mod_perl不同,在托管站点上运行CGI :: Fast非常容易,因为你可以在不重启Apache的情况下重启脚本(至少那是我要求mod_perl时我的主机告诉我的那个)。
#3
- Make sure this is actually your bottleneck
- Only import modules you need
- Make sure you aren't having to search a zillion places to find them
- Consider having processes with costly startup run hot (e.g. as a daemon) and use lighter weight CGI scripts to start them
- Look into various accelerators rather than doing full CGI from a shell (depends on what you're using to serve the pages).
确保这实际上是你的瓶颈
只导入您需要的模块
确保您不必搜索数以千计的地方来查找它们
考虑让具有昂贵启动的进程运行热(例如作为守护进程)并使用较轻的CGI脚本来启动它们
查看各种加速器,而不是从shell执行完整的CGI(取决于您用于提供页面的内容)。
#4
You could always try the less pragma:
你总是可以尝试少用的pragma:
use less 'starttime';
Of course, that is system dependent*
. Your best bet is to use mod_perl or one of the FastCGI modules: CGI::Fast, FCGI, etc.
当然,这取决于系统*。最好的办法是使用mod_perl或其中一个FastCGI模块:CGI :: Fast,FCGI等。
If it needs to be fast from the commandline you may want to move to a client/server architecture (which is all FastCGI is).
如果它需要从命令行快速,您可能想要转移到客户端/服务器架构(这是所有FastCGI)。
*
warning no systems currently implement a starttime option for the less pragma.
*警告没有系统当前为较少的pragma实现starttime选项。
#5
Well, others already have suggested that CGI might be your problem here, so I'll consider that you can't remove CGI from the picture.
好吧,其他人已经建议CGI可能是你的问题,所以我认为你不能从图片中删除CGI。
You might want to consider this old article. Apparently one source of slow startup time is a huge @INC so consolidating everything in a short PERL5LIB seem to help tremendously (that seems to be a fair assumption, but I never tried it).
您可能想要考虑这篇旧文章。显然,启动时间慢的一个原因是一个巨大的@INC,因此在短的PERL5LIB中整合所有东西似乎有很大的帮助(这似乎是一个公平的假设,但我从未尝试过)。
Alternatively (or additionally), if you don't mind paying some price at run time you can use Class::Autouse
或者(或另外),如果您不介意在运行时支付一些价格,您可以使用Class :: Autouse
Enjoy!
#6
Try using SpeedyCGI or Persistent Perl.
尝试使用SpeedyCGI或Persistent Perl。
Both implement roughly the same idea: Instead of the Perl interpreter, they a wrapper that parses the program and keep it in memory, thus saving the time required for initializing the interpreter and parsing on every run.
两者都实现了大致相同的想法:它们是一个包装器,它解析程序并将其保存在内存中,而不是Perl解释器,从而节省了在每次运行时初始化解释器和解析所需的时间。
This should work fine with the dual-environment setup you described which would/might not be the case when using CGI::Fast or mod_perl.
这应该适用于您描述的双环境设置,使用CGI :: Fast或mod_perl时可能/可能不是这种情况。
EDIT If this helps, fine. If it doesn't, you'll have to measure where your script spends its run-time.
编辑如果这有帮助,那很好。如果没有,您将不得不测量脚本在运行时的花费。
#1
Consider using CGI::Fast in order to start one perl process to handle multiple requests. It took me very little effort to change some of my big CGI scripts over to CGI::Fast. Unlike mod_perl, it's very easy to run CGI::Fast on hosting sites because you can restart your scripts without restarting Apache (at least that's what my hoster told me when I asked for mod_perl).
考虑使用CGI :: Fast来启动一个perl进程来处理多个请求。我花了很少的精力将一些大的CGI脚本改为CGI :: Fast。与mod_perl不同,在托管站点上运行CGI :: Fast非常容易,因为你可以在不重启Apache的情况下重启脚本(至少那是我要求mod_perl时我的主机告诉我的那个)。
#2
How about using mod_perl to run your scrips?
如何使用mod_perl来运行你的程序?
#3
- Make sure this is actually your bottleneck
- Only import modules you need
- Make sure you aren't having to search a zillion places to find them
- Consider having processes with costly startup run hot (e.g. as a daemon) and use lighter weight CGI scripts to start them
- Look into various accelerators rather than doing full CGI from a shell (depends on what you're using to serve the pages).
确保这实际上是你的瓶颈
只导入您需要的模块
确保您不必搜索数以千计的地方来查找它们
考虑让具有昂贵启动的进程运行热(例如作为守护进程)并使用较轻的CGI脚本来启动它们
查看各种加速器,而不是从shell执行完整的CGI(取决于您用于提供页面的内容)。
#4
You could always try the less pragma:
你总是可以尝试少用的pragma:
use less 'starttime';
Of course, that is system dependent*
. Your best bet is to use mod_perl or one of the FastCGI modules: CGI::Fast, FCGI, etc.
当然,这取决于系统*。最好的办法是使用mod_perl或其中一个FastCGI模块:CGI :: Fast,FCGI等。
If it needs to be fast from the commandline you may want to move to a client/server architecture (which is all FastCGI is).
如果它需要从命令行快速,您可能想要转移到客户端/服务器架构(这是所有FastCGI)。
*
warning no systems currently implement a starttime option for the less pragma.
*警告没有系统当前为较少的pragma实现starttime选项。
#5
Well, others already have suggested that CGI might be your problem here, so I'll consider that you can't remove CGI from the picture.
好吧,其他人已经建议CGI可能是你的问题,所以我认为你不能从图片中删除CGI。
You might want to consider this old article. Apparently one source of slow startup time is a huge @INC so consolidating everything in a short PERL5LIB seem to help tremendously (that seems to be a fair assumption, but I never tried it).
您可能想要考虑这篇旧文章。显然,启动时间慢的一个原因是一个巨大的@INC,因此在短的PERL5LIB中整合所有东西似乎有很大的帮助(这似乎是一个公平的假设,但我从未尝试过)。
Alternatively (or additionally), if you don't mind paying some price at run time you can use Class::Autouse
或者(或另外),如果您不介意在运行时支付一些价格,您可以使用Class :: Autouse
Enjoy!
#6
Try using SpeedyCGI or Persistent Perl.
尝试使用SpeedyCGI或Persistent Perl。
Both implement roughly the same idea: Instead of the Perl interpreter, they a wrapper that parses the program and keep it in memory, thus saving the time required for initializing the interpreter and parsing on every run.
两者都实现了大致相同的想法:它们是一个包装器,它解析程序并将其保存在内存中,而不是Perl解释器,从而节省了在每次运行时初始化解释器和解析所需的时间。
This should work fine with the dual-environment setup you described which would/might not be the case when using CGI::Fast or mod_perl.
这应该适用于您描述的双环境设置,使用CGI :: Fast或mod_perl时可能/可能不是这种情况。
EDIT If this helps, fine. If it doesn't, you'll have to measure where your script spends its run-time.
编辑如果这有帮助,那很好。如果没有,您将不得不测量脚本在运行时的花费。