vit has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I am running my web tool in 2 instances in AWS using the load balancer. But it's definitely not an AWS issue. Probably I am doing it in a wrong way. This is user records coded like this:
sub user_record { my $count = hit_counter($user_count_file); open (IF, ">>$user_records_file") or die "Cannot open $user_records_fi +le\n"; my $str = "----------------------------------------------------------- +\n"; $str .= "count: $count\n"; $str .= mytime()."\n"; $str .= "ip adderss: $ip_ad\n"; $str .= "User Agent: $http_user_agent\n"; $str .= "Query: $query\n"; $str .= "Search Type: $search_type\n"; print IF $str; close IF; return; } #================= sub hit_counter { my ($countfile) = @_; open(CNF,"<$countfile"); my $count = <CNF>; close(CNF); chop($count); $count++; open(CNF,">$countfile"); print CNF "$count\n"; close(CNF); return $count; }
I am getting some results in a wrong order:
----------------------------------------------------- count: 266 2011:6:1:13:49:31 ip adderss: 24.60.87.27 User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; Query: Search Type: ----------------------------------------------------- count: 270 2011:6:1:13:50:2 ip adderss: 24.60.87.27 User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; Query: Search Type: ----------------------------------------------------- count: 269 2011:6:1:13:49:55 ip adderss: 24.60.87.27 User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; Query: Search Type: -----------------------------------------------------
There is definitely an issue with appending to the same file. Any idea how to fix it?

Replies are listed 'Best First'.
Re: Writing to the same file from different instances
by jwkrahn (Abbot) on Jun 01, 2011 at 18:23 UTC
Re: Writing to the same file from different instances
by Anonymous Monk on Jun 01, 2011 at 18:21 UTC
Re: Writing to the same file from different instances
by locked_user sundialsvc4 (Abbot) on Jun 02, 2011 at 14:39 UTC

    Another way to handle a problem like this is to create a simple server that reads from (say...) a socket, and writes what it reads into the file.   That way, only one process is actually writing to the file, and it’s doing it asynchronously.   If you have a bunch of processes who basically need “to log something and move on,” but who do not need to subsequently and immediately read what is to be written, then this is a useful technique.   (Operating systems usually delegate a “logger” system process for this very purpose and reason.)   You cannot predict the exact order in which the writes will occur, as before, but the entire messy issue of “locking” is completely avoided.