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.


In reply to Re: Slow worker threads by ig
in thread Slow worker threads by pome23

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.