开发者

Perl script: how to realize if a process has finished

开发者 https://www.devze.com 2023-03-03 09:20 出处:网络
This is what I have created so far regarding your advice: #!/usr/bin/perl -开发者_JAVA技巧w use strict;

This is what I have created so far regarding your advice:

#!/usr/bin/perl -开发者_JAVA技巧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


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.


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.


When your external process is complete, <$pipe> will return undef and your while loop

while (my $line = <$pipe>) { ... }

will finish (Until then, <$pipe> will block while waiting for input to be available). Therefore you can tell that the job is done when the while loop is done. If you can, you'll want to configure your external command (/love/peace/harmony/./lovepeace.bash) to not buffer its output.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号