How does the number of bananas get updated on the nodes? Is there any calculation going on on the nodes - or just a summing of the bananas? How often do the numbers change? Do you know the number of bananas at the time the number changes?
You may be lucky enough to be able to write out a static HTML page containing the number of bananas. Nothing will be able to query as fast as this.
But even if you do have to use a cgi script or mod_perl (I'd heavily suggest mod_perl), the following algorithm will work fine - and there is no threading or forking.
my @socks;
for my $host (@hosts) {
my $sock = IO::Socket::INET->new(PeerAddr => $host,
PeerPort => 80);
die "$host failed: $!" if ! $sock;
print $sock "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
push @socks, $sock;
}
my $sum = 0;
for my $sock (@socks)
local $/ = '';
my $resp = <$sock>
$sum += parse_resp($resp);
}
This code works and is as fast as your slowest server because: You have asked all of the servers the question which takes near 0 time (assuming connection time isn't the issue). Each of the servers can take however long they need to generate the response. Then you begin asking for the answers. As long as the transport isn't an issue (on a local network it better not be), then you basically asked all of the servers for the answer and got the results back in about the time that it took for the slowest server to generate the response. Servers that get done more quickly simply have the result waiting for you in the open $sock handles. Ones that take longer will block while you read $sock - but that is OK because other servers will still be creating their responses while you wait for the slow one.
You can try things more complicated - but chances are very, VERY high that the extra complication will introduce its own overhead and bottlenecks and won't deliver the results any faster. (If connection time or transport are issues then all of this goes out the window - but it is doubtful connection or transport will be the bottleneck for this application).
my @a=qw(random brilliant braindead); print $a[rand(@a)];
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.