in reply to RE: Sample Random (Code)
in thread Reading files sequentially

damian What you are dealing with is more of a concurrency issue.

I suggest trying to reduce the webpage to one call of this script. However, if you don't you will have to help your script out more than you do now.

Basically you have to use file locking, and then also write your script like an iterator function. That is, where you see the while loops you need remove them and instead store state information in files that will allow you to simulate the while loop over multiple calls of the script.

Here is sample code to give you an idea of what I'm talking about (because it's getting late and I'm low on caffeine :) -- so I know I'm not entirely clear).

simple.pl

#!/usr/bin/perl #This program counts from 1 to 10 my $count = 1; while($count <= 10) { print $count,"\n"; $count++; }

iterator.pl

#!/usr/bin/perl #This program prints out 1 to 10, but only one number #per call to the program if (not -e "/tmp/iterator-state") { `echo 1 > /tmp/iterator-state`; } open(S, "/tmp/iterator-state"); chomp(my $count = <S>); print $count,"\n"; $count++; close(S); if ($count <= 10) { `echo $count > /tmp/iterator-state`; } else { unlink "/tmp/iterator-state"; }

The major problem with this last approach is that it's very very very BAD. That is, you need to do alot of locking that I'm not doing in the example, and whole sorts of other things can go wrong.

Therefore, I suggest you change your requirements. Either do it like my original sample does (that is, run the script only once and get all the results then). Alternatively, you can split the ad file into three separate files, and then run your original code on it (but of course once on each file).

The last method I just mentioned can be jury-rigged to act more like what you want. You can periodically change the three files to be three new files with different subsets. That way you don't have the same 1/3 of the ad's appear on each 3 ad's.

Enjoy!
Gryn

Replies are listed 'Best First'.
RE: Call lots of em (uck)
by damian (Beadle) on Sep 15, 2000 at 09:09 UTC
    thanks again gryng, at least i know where to start.