CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Rotating data through alternate functions

 



Azzir
Novice

Aug 7, 2008, 10:21 PM

Post #1 of 6 (1150 views)
Rotating data through alternate functions Can't Post

Greetings!

First time posting to a Perl forum, so "hello" and of course "please be kind".

I'm basically trying to rotate through a series of values (located in two seperate files) to be run alternately by two different functions.

I have written a script that tests a basic CreateSubscription/DeleteSubscription application. In order to cancel a subscription, I need to ensure that the subscription is in the database, and vice-versa. To do this, I have created two flat files "CreateSub.txt" and "DeleteSub.txt" which contain unique subscription identifiers (I refer to these files as 'rotation logs'.

eg. CreateSub.txt contains the ID's of subscriptions not yet in the database:

0001
0002
0003
0004
0005

DeleteSub.txt contains the ID's of subscriptions that are already in the database:

0006
0007
0008
0009
0010

Each time the CreateSubscription test is run, it will do the following:

* open the 'CreateSub.txt' file using the 'Tie::File' module
* splice out the first line (In this case '0001')
* close/untie the file handle
* execute the 'CreateSubscription' function
* open the 'DeleteSub.txt' file using the 'Tie::File' module
* push the now created ID (0001) to the bottom of the file
* close/untie the file handle

As you can no doubt guess, the files now contain the following:

CreateSub.txt:
0002
0003
0004
0005

DeleteSub.txt
0006
0007
0008
0009
0010
0001

The DeleteSubscription test runs at an identical interval, in much the same manner, thereby creating a environment that makes sure there is always valid data to be run for each of the operations...

...in a perfect world :-P

In this world however, where I have multiple automated scripts calling upon the same rotation logs, I am seeing timeouts and data corruption.

e.g. CreateSub.txt
0005
0005
0006
0007
0007
0007

Sorry if I've been a little long winded in this... has anybody had any experience in a similar situation? I'm on the verge of pulling out what little hair I have, and am more than open to suggestions! (am more than happy to post code, however I thought that might be a bit of overkill for my first post)

Mucho Grassy Ass! - Azzir :-)


Azzir
Novice

Aug 7, 2008, 10:23 PM

Post #2 of 6 (1149 views)
Re: [Azzir] Rotating data through alternate functions [In reply to] Can't Post

PS. Sorry if this should have been in the beginner forum :P


KevinR
Veteran


Aug 7, 2008, 10:33 PM

Post #3 of 6 (1147 views)
Re: [Azzir] Rotating data through alternate functions [In reply to] Can't Post

are you locking the files? Something like:


Code
my $o = tie my @array, "Tie::File", filename; 
$o->flock;
[perform operations here]
undef $o;
untie @array;

-------------------------------------------------


Azzir
Novice

Aug 7, 2008, 10:38 PM

Post #4 of 6 (1146 views)
Re: [KevinR] Rotating data through alternate functions [In reply to] Can't Post

Thanks Kevin... I tried flock however all i managed to do last time was make all my files readonly... I suspect I missed the undef.

Thanks heaps will try that now :-)


KevinR
Veteran


Aug 7, 2008, 11:07 PM

Post #5 of 6 (1145 views)
Re: [Azzir] Rotating data through alternate functions [In reply to] Can't Post

You only use undef if you use Tie::File like I showed, creating an object ($o). Using Tie::File to append a line to a file is not the best idea in my opinion, just open the file in append mode ">>". Tie::File is good if you are working with really big files, but besides that I tend stay away from it because of performance issues.

From the Tie::File documentation:


Quote
CONCURRENT ACCESS TO FILES

Caching and deferred writing are inappropriate if you want the same file to be accessed simultaneously from more than one process. Other optimizations performed internally by this module are also incompatible with concurrent access. A future version of this module will support a concurrent => 1 option that enables safe concurrent access.


might explain the problems you are having.
-------------------------------------------------


Azzir
Novice

Aug 11, 2008, 7:11 PM

Post #6 of 6 (1113 views)
Re: [KevinR] Rotating data through alternate functions [In reply to] Can't Post

Thanks Kevin, that worked a treat :-)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives