|
Posted by Frank Seitz on March 5, 2008, 2:25 am
Please log in for more thread options fishfry wrote:
>>howa wrote:
>>>
>>>I have a CGI which is targeted to log some messages to a file, since
>>>Apache are running at multi-process, are there any modules which can
>>>handle file locking issue if I received many requests?
>>
>>Why use a module? It's simple to implement with Perl's
>>file locking interface. See "perldoc -f flock".
>>
>>use strict;
>>use warnings;
>>
>>use Fcntl qw/:flock/;
>>
>>open my $fh,'>>','file.log' or die $!; # open file
>>flock $fh,LOCK_EX or die $!; # lock file
>>print $fh "message\n" or die $!; # write file
>>close $fh or die $!; # close file
>
> Of course in real life you don't want your cgi to die just because it
> can't obtain a lock on the logfile. It should wait and retry, or just
> return a normal response to the enduser.
It is example code. The OP can do whatever he want
in case of an error.
> Losing a log message is less important than servicing the enduser.
It depends on the application.
> Writing through a logging service
> is the way to solve this issue in real world, high volume applications.
Yes, but in this case a simple solution may be more appropiate.
Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/ Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
|