Redirection whatever be the forked life

Y

Yohan N. Leder

Hi,

Newbie in Perl and don't know how to do with this piece of script. I
would like it redirects to mainboard.cgi without waiting for long forked
process to be completed. Is it possible ? Knowing that in the real
script the storit sub does a GET to a remote counter script which
generates a log file.

#!/usr/bin/perl -w
use strict;

# normally use CGI, but just a quick test script here
print "CONTENT-TYPE: text/html; Charset=UTF-8\n\n";

# ... here, some *quick* treatments (less than 10 seconds)...

defined(my $pid = fork) or die "cannot fork : $!";
unless ($pid) {
# storit being a *long* process (say 1 or 2mn) without any print
storit();
exit 0;
}

# ... here, some checking about previous treatment

print "Successfull treatment ! Redirection in 4 secondes...";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"4; URL=mainboard.cgi\">";
exit 0;

sub storit {
# simulate a long process (until 3mn in reality)
for (my $idx=1;$idx<10;$idx++) {
sleep 1;
}
}

And with this mainboard.cgi :

#!/usr/bin/perl -wT
use strict;
print "CONTENT-TYPE: text/html; Charset=UTF-8\n\n";
print "... main board ...";
exit 0;

Yohan N. Leder
 
D

David Squire

Yohan said:
Hi,

Newbie in Perl and don't know how to do with this piece of script. I
would like it redirects to mainboard.cgi without waiting for long forked
process to be completed. Is it possible ? Knowing that in the real
script the storit sub does a GET to a remote counter script which
generates a log file.

Yes. You need to make the parent process (which gets a TRUE value for
pid) be the one that redirects to somewhere else (easily done using the
CGI module). This seems to be what you are doing, since you call
storit() unless $pid.

What is going wrong for you? You do not report any errors or unwanted
behaviour.

DS
 
D

David Squire

David said:
Yes. You need to make the parent process (which gets a TRUE value for
pid) be the one that redirects to somewhere else (easily done using the
CGI module). This seems to be what you are doing, since you call
storit() unless $pid.

Premature posting, sorry. It's not as simple as that. See perdoc -f fork
for issues such as zombie creation (if the parent does not wait for the
child, which is what would happen in the scenario above), and connection
of file system descriptors such as STDIN and STDOUT.

Hmmm. Perhaps you could make the child be the one that redirects (since
it should finish ASAP and can be waited for by the other), inheriting
the socket connected IO, and reopen those in the parent. Just
speculating though...

DS
 
A

A. Sinan Unur

X

xhoster

Yohan N. Leder said:
Thanks, but, unless error, my case doesn't fit what they talk about.
They say : "The CGI protocol is wonderful for the remote execution of
short tasks. But how do you execute a longer task? You can't just have
the task slowly executing without giving some kind of feedback to the
user, and eventually Apache will get bored and have the connection time
out anyway." And, in my case, I don't want to give feedbacl to the user
about forked process, but wish that the GET, proceeded in forked
process, doesn't block the HTML redirection written by the main process.

HTML redirection is just another form of feedback. So it is true that
that code can't just be naively copied and pasted, but it gives you
everything you need to write your own code suited for your needs.

Xho
 
Y

Yohan N. Leder


Thanks, but, unless error, my case doesn't fit what they talk about.
They say : "The CGI protocol is wonderful for the remote execution of
short tasks. But how do you execute a longer task? You can't just have
the task slowly executing without giving some kind of feedback to the
user, and eventually Apache will get bored and have the connection time
out anyway." And, in my case, I don't want to give feedbacl to the user
about forked process, but wish that the GET, proceeded in forked
process, doesn't block the HTML redirection written by the main process.
 
Y

Yohan N. Leder

Premature posting, sorry. It's not as simple as that. See perdoc -f fork
for issues such as zombie creation (if the parent does not wait for the
child, which is what would happen in the scenario above), and connection
of file system descriptors such as STDIN and STDOUT.

Hmmm. Perhaps you could make the child be the one that redirects (since
it should finish ASAP and can be waited for by the other), inheriting
the socket connected IO, and reopen those in the parent. Just
speculating though...

DS

Well, in fact, the thing that does forked process my be long is that the
remote cgi call (GET) by the storit sub can be busy or unavailable
sometimes. Also, I would like that the main process doesn't take care at
all of the forked one, and that the HTML redirection (Refresh) happens
even if the GET didn't succeeded... But maybe it's unfaisable. Don't
know.
 
A

A. Sinan Unur

S

Sumo Wrestler (or just ate too much)

Yohan said:
[...] the thing that does forked process my be long is that the
remote cgi call (GET) by the storit sub can be busy or unavailable
sometimes. Also, I would like that the main process doesn't take care at
all of the forked one, and that the HTML redirection (Refresh) happens
even if the GET didn't succeeded... But maybe it's unfaisable. Don't
know.

Maybe something like this could work for you:

use strict;

print "Content-Type: text/html\n";
print "Location: index.html\n";
print "\n";

system ("/usr/local/bin/my-longproc &");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top