Solved

Fetch each new page in a new process

Posted on 2001-08-17
11
375 Views
Last Modified: 2008-02-26
I am fetching a page to get a part of that site!
I am using the following code:

#!/usr/local/bin/perl

# read en write currency convert file from www.oada.com
$output_url = "http://www.oanda.com/converter/classic?value=1&exch=NLG&expr=USD";

push(@test, "EUR","USD","GBP","NLG");
use LWP::Simple;

print "Content-type: text/html\n\n";
foreach (@test) {
     $html = get("http://www.oanda.com/converter/classic?value=1&exch=NLG&expr=$_");
     $/="\n";
     
     # Remove the \n chars from that variable.
     $html =~ s/\n//g;
     # Extract the relevent part.
     $html =~ /(.*)<\!-- conversion result starts  -->(.*)\!-- conversion result ends  -->(.*)/;
     # Set $html_text
     $html_text = $2;
     # Squeeze multiple white spaces to a single space
     $html_text =~ s/\s+/ /g;
     # Remove any HTML tags
     $html_text =~ s/<[^>]*>//g;
     # Get the currency value!!
     $html_text =~ /$$item_currency\s+=\s+([\d,]+\.?\d+)/;
     $result = $1;
     $result =~ s/,//g;

     print "<br>result: $result<br>";
}

The fetching will done by the get command.
This all is done as much times as the values in @test.
The get process will take sometimes so much time that I get the following error:

CGI Timeout
The specified CGI application exceeded the allowed time for processing. The server has deleted the process.

Is it possible to start for each get command a new process?
How to take care that the cgi timeout will not happen??
If it happens, how to start the process again (Maybe for a max. of 3 times)??

0
Comment
Question by:weversbv
  • 5
  • 5
11 Comments
 
LVL 6

Expert Comment

by:christopher sagayam
ID: 6396397
?
0
 

Author Comment

by:weversbv
ID: 6396420
Get the page with the expr EUR.
Write to screen and then start the script again with the following value (USD) etc.
I think the foreach loop has then to be removed??
0
 
LVL 5

Expert Comment

by:marecs
ID: 6396809
This script forks and thus accesses the site concurrently for each currency. It is still possible that one of the accesses takes too long and the script may time out. Try using LWP::UserAgent and set the timeout value.

#!/usr/bin/perl -w

use strict;
use LWP::Simple;
use vars qw(@test $cur $child $html_text $html);

my @test = ( "EUR","USD","GBP","NLG");
$|++; # Autoflush essential
print "Content-type: text/html\n\n";

foreach $cur (reverse @test) {
    if ($child = fork) {
        waitpid($child,0);
        print $html_text if defined $html_text;
        exit;
    }
    else {
        die "fork failed" unless $child == 0;
        $html = get("http://www.oanda.com/converter/classic?value=1&exch=NLG&expr=$cur");
        $html =~ /<!-- conversion result starts  -->(.*)<!-- conversion result ends  -->/s;
        $html_text = $1;
    }
}

print $html_text; # Last child with no child of its own
0
 
LVL 5

Expert Comment

by:marecs
ID: 6396837
If the server killed you then that's it. You cannot start yourself up again. The user will have to initiate a new request from his browser. Why not store a cache of the exchange rates and fall back to these values when a timeout occurs?

0
 

Author Comment

by:weversbv
ID: 6397004
What do you mean marecs??
Can you give me an example!

By the way I tried your code, but it will not work on NT (Windows2000).
The Unsupported function fork function is unimplemented...
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:weversbv
ID: 6397025
How to use and set the LWP::UserAgent and set the
timeout value.
0
 
LVL 5

Expert Comment

by:marecs
ID: 6397055
Then I am very sorry I put in the effort. My solution is a UNIX only one unfortunately.

If you use LWP::UserAgent rather than LWP::Simple there is an option to specify the timeout of a request. What you probably want  to prevent is that nasty "CGI Timeout" error. So if your request times out, it would be handy to have values you can fall back to. This can be achieved by writing the exchange rates to a file every time you were able to get them, and then read those values when a timeout occurs.

Sorry, no time for an example. look at perldoc LWP::UserAgent
0
 
LVL 5

Expert Comment

by:marecs
ID: 6397072
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$request = HTTP::Request->new('GET', 'http://www.msn.com/');
$ua->timeout(5); # 5 seconds
$response = $ua->request($request);
$response or die "Timed out";
0
 

Author Comment

by:weversbv
ID: 6397088
I will increase to 300 point for a example!
0
 

Author Comment

by:weversbv
ID: 6397113
I tried this:

#!/usr/local/bin/perl

$|++;

use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$request = HTTP::Request->new('GET', 'http://www.msn.com/');
$ua->timeout(5); # 5 seconds
$response = $ua->request($request);
$response or die "Timed out";

print "Content-type: text/html\n\n";
print "response: $response<br>";
print "request: $request<br>";

But this will be the result:

response: HTTP::Response=HASH(0x8647ac)
request: HTTP::Request=HASH(0x3a96c8)

What am I doing wrong?
0
 
LVL 5

Accepted Solution

by:
marecs earned 300 total points
ID: 6397157
print "response: ", $response->content, "<br>";

So you could use your original script with
$html = $response->content;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now