File locking (and resource locking in general) is only needed when resource contention is likely to happen and the resulting problems outweigh the problems you have by implementing the locking mechanism.

In your case, you only use top.html for reading in the code, and it seems to me like top.html is part of some search-result template. If this is the case, then top.html does not change often, and is written to only when somebody copies over a new version of it. The results of reading a half-written top.html would be that somebody gets a broken page, which is fixed by the moment they reload it. If all of that were the case, locking would be overhead that has no practical value, as people will see broken results because of bad connections more often anyway.

If you had a second CGI which updates top.html much more often (say every minute), your chance of producing broken HTML would increase. You could then still try to avoid locking with some fancy file renaming tricks. But the risk of getting a lock conflict because one script tries to write and the other tries to read from the half-written file is still there and you're heading into lock-land.

The case where you will absolutely need locking is, when you have two (or more) scripts (or two instances of the same script) that try to write to the same file at the same time. In this case you will always need locking.


perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

In reply to Re: Do I need to give a flock? by Corion
in thread Do I need to give a flock? by jerrygarciuh

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.