in reply to Slow worker threads
This is not related to your performance problem, but I wonder if flock is doing what you think it is. Consider the following:
use strict; use warnings; use threads; use Fcntl qw(:flock); open(FILE, ">>/tmp/test.txt"); for(0..9) { threads->new(\&worker, $_); } $_->join for threads->list; sub worker { my $x = shift; flock(FILE, LOCK_EX); print "$x: locked file\n"; sleep(10); flock(FILE, LOCK_UN); print "$x: unlocked file\n"; return; }
Which produces
0: locked file 1: locked file 2: locked file 3: locked file 4: locked file 5: locked file 6: locked file 7: locked file 8: locked file 9: locked file 0: unlocked file 1: unlocked file 2: unlocked file 3: unlocked file 4: unlocked file 5: unlocked file 6: unlocked file 7: unlocked file 8: unlocked file 9: unlocked file
If you are locking against other processes changing the file between your writes, you should probably be seeking to end of file between locking and writing.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Slow worker threads
by spx2 (Deacon) on Jul 18, 2009 at 17:56 UTC | |
by spx2 (Deacon) on Jul 18, 2009 at 19:20 UTC |