Perl脚本:如何知道进程是否已完成
这是我到目前为止根据您的建议创建的内容:
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
#some variables
my $message = "please wait, loading data...\n";
#First build the web page
print header;
print start_html('Hello World');
print "<H1>we need love, peace and harmony</H1>\n";
print "<p>$message</p>\n";
#Establish a pipeline between the bash and my script.
my $bash_command = '/love/peace/harmony/./lovepeace.bash';
open(my $pipe, '-|', $bash_command) or die $!;
while (my $line = <$pipe>){
# Do something with each line.
print "<p>$line</p>\n";
}
#when is the job done...?
print end_html;
当我在浏览器中调用该 .pl 脚本时,一切正常:-) 但我仍然有几个问题:
当我调用该网站时,它正忙于加载一些内容来自管道的值。由于大约有 10 个值,速度相当快(2-4 秒),但如果我有 100 多个值,用户必须等待一段时间。由于我不能有进度条,我应该向用户提供信息。
例如:“正在加载数据,请稍候...”
当工作完成时,此消息应该显示:“工作完成”或类似的内容。
• 我如何知道该过程是否完成?
• 如果作业完成,我可以重新加载页面吗?
• 是否有机会在这个 perl-CGI 中使用我自己的样式表
问候,
JJ
This is what I have created so far regarding your advice:
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
#some variables
my $message = "please wait, loading data...\n";
#First build the web page
print header;
print start_html('Hello World');
print "<H1>we need love, peace and harmony</H1>\n";
print "<p>$message</p>\n";
#Establish a pipeline between the bash and my script.
my $bash_command = '/love/peace/harmony/./lovepeace.bash';
open(my $pipe, '-|', $bash_command) or die $!;
while (my $line = <$pipe>){
# Do something with each line.
print "<p>$line</p>\n";
}
#when is the job done...?
print end_html;
When I call that .pl script in my browser, everything works nice :-) But a few questions are still on my mind:
When I call this website, it is busy loading some values from the pipe. Since there are about 10 Values its rather quick (2-4 seconds) But if I would have 100+ Values, the user has to wait a while. Since I cannot have a progress bar, I should provide an information to the user.
Like:"Loading data, please wait..."
And when the job is done, this message should say: "Job done" or something similar.
• How do I realize if the process is finnished?
• Can I reload the page if the job is done ?
• Is there any chance of using my own stylesheet wihtin this perl-CGI
Regards,
JJ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Randal Schwartz 的通过 CGI 观看长进程在这里可能会有所帮助。
至于使用您自己的样式表,您只需在要发出的
...
部分中指定即可。Randal Schwartz's Watching long processes through CGI might be helpful here.
As for using your own stylesheet, you can just specify that in the
<head>...</head>
section you are emitting.将其实现为 ajax 可能更有意义。允许页面完全渲染,然后使用 ajax 请求来查看进度。如果在该过程完成之前您的页面没有看到 ,则您的页面可能无法正常显示。
编辑:抱歉,想法不完整。查看逐行输出的更彻底的方法是将该脚本作为后台进程启动,将输出写入日志,然后让 ajax 服务器代码(一个单独的 CGI)返回到目前为止处理的行和一个标志如果进程已经退出。
It might make more sense to implement this as ajax. Allow the page to full render and then have an ajax request to watch the progress. Your page may not display nicely if it doesn't see the until the process is finished.
EDIT: Sorry, incomplete thought. A more thorough approach to see the line-by-line output would be to kick off that script as a background process writing output to a log, then having the ajax server code (a separate CGI) return the lines processed so far and a flag if the process has exited.
当您的外部进程完成时,
<$pipe>
将返回undef
并且您的 while 循环将完成(在此之前,
<$pipe>
> 将在等待输入可用时阻塞)。因此,当while
循环完成时,您可以知道工作已完成。如果可以的话,您需要将外部命令 (/love/peace/harmony/./lovepeace.bash
) 配置为不缓冲其输出。When your external process is complete,
<$pipe>
will returnundef
and your while loopwill finish (Until then,
<$pipe>
will block while waiting for input to be available). Therefore you can tell that the job is done when thewhile
loop is done. If you can, you'll want to configure your external command (/love/peace/harmony/./lovepeace.bash
) to not buffer its output.