Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: [threads] Open a file in one thread and allow others to write to it

by gulden (Monk)
on Nov 16, 2009 at 12:42 UTC ( #807427=note: print w/replies, xml ) Need Help??


in reply to [threads] Open a file in one thread and allow others to write to it

A simplified approach without log file rotation:
#!/opt/coolstack/bin/perl -w use strict; use warnings; use threads; use Thread::Queue; my $file = "logfile.log"; my $queue = Thread::Queue->new() or die "$!"; my $thr = threads->create( sub{ my $q = shift; my $line; open(my $fh,">>", $file) or die "Cant open file $! [$file]"; print $fh "$line\n" while $line = $queue->dequeue(); }, $queue ); my @thrs; push @thrs , threads->create(sub{ $queue->enqueue(threads->tid ."> Log Message: $_") for (1..100); }) for (1..10); $_->join for @thrs; $queue->enqueue(undef); # End Logger Thread $thr->join; exit;
A contentious debate is always associated with a lack of valid arguments.
  • Comment on Re: [threads] Open a file in one thread and allow others to write to it
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://807427]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2018-08-21 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (197 votes). Check out past polls.

    Notices?