2011-12-13 05:02



This is more of a question about best practices. My server allows users to convert fonts from one format to another using FontForge. To prevent collisions, only one font conversion is allowed at any given instance.

When one user initiates a conversion, a PID file is created which acts as a lock. If another user tries to initiate a conversion while the first conversion is still running, then the script will pause for a moment and check for the PID file again.

This repeats until the first process removes the PID file, thus unlocking access to FontForge. However, if the first user cancels the script before it finishes (by pressing the stop button on their browser), then the script exits before the PID file is removed. The second user will never be able to proceed.

What is the best way to handle this?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • douya2007 douya2007 10年前

    Looking at this PHP Man page:


    I quote:

    You can decide whether or not you want a client disconnect to cause your script to be aborted. Sometimes it is handy to always have your scripts run to completion even if there is no remote browser receiving the output. The default behaviour is however for your script to be aborted when the remote client disconnects. This behaviour can be set via the ignore_user_abort php.ini directive as well as through the corresponding php_value ignore_user_abort Apache httpd.conf directive or with the ignore_user_abort() function. If you do not tell PHP to ignore a user abort and the user aborts, your script will terminate.

    The one exception is if you have registered a shutdown function using register_shutdown_function(). With a shutdown function, when the remote user hits his STOP button, the next time your script tries to output something PHP will detect that the connection has been aborted and the shutdown function is called. This shutdown function will also get called at the end of your script terminating normally, so to do something different in case of a client disconnect you can use the connection_aborted() function. This function will return TRUE if the connection was aborted.

    点赞 评论 复制链接分享
  • dsj1961061 dsj1961061 10年前

    Stop watching for the PID file to go away. Start using flock so the operating system will clean up the lock if you exit prematurely.

    Example code from http://tuxradar.com/practicalphp/8/11/undefined:

        $fp = fopen("foo.txt", "w");
        if (flock($fp, LOCK_EX)) {
            print "Got lock!
            flock($fp, LOCK_UN);

    Also, http://php.net/manual/en/function.flock.php.

    点赞 评论 复制链接分享