Scott203 has asked for the wisdom of the Perl Monks concerning the following question:
When a user opens a page on my webserver (lets call that page "script A"), the script checks something and then has to execute another script (script B) if necessary based on the check. Right now, the user has to wait for a couple minutes for script B to execute before script A can complete and send the page to the user. I need to know if script A can initiate script B to run in the background, independent of the web page being printed. Thanks
Re: executing other scripts while printing a webpage
by tadman (Prior) on Jul 30, 2001 at 06:58 UTC
|
The catch is that you always have to return something,
even if that something is "nothing". Some sites feature
a waiting page which refreshes frequently, waiting on output
from something. It might feature a pleasant animated GIF,
and hopefully not something along the lines of WAIT
with the horrific <BLINK> tag.
A quick way of doing this is to create script A which,
when run the first time will fork off script B, and then
refresh to itself with a special parameter. Script B
will write to a certain filename in /tmp, for example,
and script A, when called, will check for this file. When
this file is there, script A prints it, possibly deletes
it, and stops refreshing. The name of the file is encoded
in the special parameter. If, for example, the parameter
"_" was set to "fZdaPXzAQ", then you would be waiting for
the file "/tmp/fZdaPXzAQ.out". The choice of name is
arbitrary.
There are a couple of things you will want to note:
- You might want to write a separate program to clean
out any stale /tmp entries instead of leaving this up to
script A. This way the user can reload the page and still
get output, without you having to process all over again.
- Your script B should not write directly to the /tmp
file, but should create a preliminary version which it
writes to, then renames it at the end to the proper name.
This will prevent script A from jumping the gun and printing
when script B has only written half its output.
| [reply] |
|
hmm, well the thing is that script A does not need anything at all from script B to execute. Most of the time, script A wont even call script B, but when it does, it does not have to wait for script B to complete, but it does. I think I need to be more clear.. script A simply prints the webpage, and also checks to see if a certain database file is too old. If that file is old, script A calls script B (currently using server(scriptB.cgi);) which downloads a fresh database file from a remote site (this takes a couple minutes cause its a big file). Script A COULD use the old db file until script B is done, but it doesnt because it waits for the server call to complete.
| [reply] |
|
| [reply] [d/l] |
Re: executing other scripts while printing a webpage
by tachyon (Chancellor) on Jul 30, 2001 at 10:47 UTC
|
There was a really good discussion of doing this sort of thing here Searching large files before browser timeout
which includes (amongst other things) a node I wrote one way to do this and also
a link to one of merlyn's columns on the same subject which covers the topic in detail.
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
| [reply] |
Re: executing other scripts while printing a webpage
by synapse0 (Pilgrim) on Jul 30, 2001 at 09:36 UTC
|
well.. if you're using system to call the script, and you're on a unix machine.. you can always make it run in the background with the shell & indicator : system("foo.pl &"); ...
-Syn0 | [reply] [d/l] |
Re: executing other scripts while printing a webpage
by cLive ;-) (Prior) on Jul 30, 2001 at 19:09 UTC
|
include script B as a sub and fork - quick and dirty...
# print cgi response as usual, then...
my $pid;
if ($pid = fork) {
# display run, so exit
exit(0);
}
elsif (defined $pid) {
# run script b function if $run_script_b true...
if ($run_script_b) {
script_B();
}
}
else {
# raise an error coz the fork failed
...
}
Works for me. My script starts an FTP install that can take up to half an hour to run, depending on network connection.
cLive ;-) | [reply] [d/l] |
|
|