PHP flock() performance, good or bad?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • writeson@charter.net

    PHP flock() performance, good or bad?

    Hi all,

    I've got a PHP program that I've added flock() to in order to protect
    multiple scripts trying to write to the same file. After I added the
    flock() to the code the performance of the code went way down. Can
    anyone tell me if calling flock() on a file handle is particularly
    slow?

    Thanks,
    Doug

  • Micha³ Wo¼niak

    #2
    Re: PHP flock() performance, good or bad?

    One quick glance of an experienced eye allowed to understand the blurred
    and almost unreadable writeson@charte r.net's handwriting:
    [color=blue]
    > I've got a PHP program that I've added flock() to in order to protect
    > multiple scripts trying to write to the same file. After I added the
    > flock() to the code the performance of the code went way down. Can
    > anyone tell me if calling flock() on a file handle is particularly
    > slow?[/color]

    Dunno about the performance of the flock() function by itself, but be
    sure to check whether there is no other script (or maybe a second
    invocation of this script?) that locks the file and causes your script
    to wait for an unlock.
    What I mean is: you have a script "a.php", which locks the file, writes
    to it, and unlocks it. Now imagine a few surfers visit your page one
    right after the other. What happens is:
    1). Surfer 1 visits your page, a.php locks the file, starts writing;
    2). Surfer 2 visits your page, a.php cannot write to the file, waits;
    3). Surfer 3 visits your page, same as above.
    When 1). finishes to write and unlocks, 2). locks and writes and 3).
    still waits. Now, the queue can get really long and the time to wait can
    get really huge when you start to get ~20 visits and it will be getting
    worse.

    Cheers
    Mike

    Comment

    • NC

      #3
      Re: PHP flock() performance, good or bad?

      write...@charte r.net wrote:[color=blue]
      >
      > I've got a PHP program that I've added flock() to in order
      > to protect multiple scripts trying to write to the same file.
      > After I added the flock() to the code the performance of the
      > code went way down. Can anyone tell me if calling flock() on
      > a file handle is particularly slow?[/color]

      Most likely, the reason is not in the flock() function itself,
      but in the fact that multiple scripts (or multiple instances
      of the same script) are trying to write to the file. Since
      it is locked, they have to wait until it is unlocked.

      You might want to consider changing from storing data in a
      file to storing it in a database. If you are on Unix and
      have no plans to deploy on Windows, another alternative,
      semaphores, is available to you.

      Cheers,
      NC

      Comment

      • mlemos

        #4
        Re: PHP flock() performance, good or bad?

        Hello,

        on 04/21/2005 10:45 AM writeson@charte r.net said the following:[color=blue]
        > I've got a PHP program that I've added flock() to in order to protect
        > multiple scripts trying to write to the same file. After I added the
        > flock() to the code the performance of the code went way down. Can
        > anyone tell me if calling flock() on a file handle is particularly
        > slow?[/color]

        Flock can be very fast if you are not trying to update the file too
        often. You should use shared lock mode for reading and exclusive lock
        mode for updating. If you always use exclusive lock for reading it will
        be very slow.

        You may want to take a look at this class that uses flock to accessing
        files to cache content.




        --

        Regards,
        Manuel Lemos

        PHP Classes - Free ready to use OOP components written in PHP
        Free PHP Classes and Objects 2026 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


        PHP Reviews - Reviews of PHP books and other products


        Metastorage - Data object relational mapping layer generator

        Comment

        • writeson@charter.net

          #5
          Re: PHP flock() performance, good or bad?

          NC,

          NC wrote:[color=blue]
          >
          > Most likely, the reason is not in the flock() function itself,
          > but in the fact that multiple scripts (or multiple instances
          > of the same script) are trying to write to the file. Since
          > it is locked, they have to wait until it is unlocked.
          >
          > You might want to consider changing from storing data in a
          > file to storing it in a database. If you are on Unix and
          > have no plans to deploy on Windows, another alternative,
          > semaphores, is available to you.
          >
          > Cheers,
          > NC[/color]

          Thanks for the response, appreciated. One thing I didn't make clear,
          and probably should have, is that writing this file only happens when
          this page is called in a 'build' mode. That is the page does two
          things, it builds the resulting HTML as a static HTML file and saves
          it, and it knows how to display that page. Kind of a home grown caching
          system. The end result is that once the static HTML is built, writing
          the file shouldn't happen again when a user views the page.

          Doug

          Comment

          Working...