PHP mutual exclusion (mutex)

前端 未结 5 851
半阙折子戏
半阙折子戏 2020-11-28 09:37

Read some texts about locking in PHP.
They all, mainly, direct to http://php.net/manual/en/function.flock.php .

This page talks about opening a file on the hard-

相关标签:
5条回答
  • 2020-11-28 09:50

    The question is: Where will you store the stats that the FTP servers are pushing with POST to your update.php file? If it's a local file, than ircmaxell in the second post has answered you. You can do this with a mutex as well - the semaphore functions. Another solution is to use MySQL MyISAM table to store the stats and use something like update info_table set value = value + 1. It should lock the table, and serialize your requests, and you will have no problems.

    0 讨论(0)
  • 2020-11-28 09:54

    PHP doesn't support multithreading, every request (and therefore every PHP script) will be executed in only one thread (or even process, depending on the way you run PHP).

    0 讨论(0)
  • 2020-11-28 10:06

    Well, most of PHP runs in a different process space (there are few threading implementations). The easy one is flock. It's guaranteed to work on all platforms.

    However, if you compile in support, you can use a few other things such as the Semaphore extension. (Compile PHP with --enable-sysvsem). Then, you can do something like (note, sem_acquire() should block. But if it can't for some reason, it will return false):

    $sem = sem_get(1234, 1);
    if (sem_acquire($sem)) {
        //successful lock, go ahead
        sem_release($sem);
    } else {
        //Something went wrong...
    }
    

    The other options that you have, are MySQL user level locks GET_LOCK('name', 'timeout'), or creating your own using something like APC or XCache (Note, this wouldn't be a true lock, since race conditions could be created where someone else gets a lock between your check and acceptance of the lock).

    Edit: To answer your edited question:

    It all depends on your server configuration. PHP May be run multi-threaded (where each request is served by a different thread), or it may be run multi-process (where each request is served by a different process). It all depends on your server configuration...

    It's VERY rare that PHP will serve all requests serially, with only one process (and one thread) serving all requests. If you're using CGI, then it's multi-process by default. If you're using FastCGI, it's likely multi-process and multi-thread. If you're using mod_php with Apache, then it depends on the worker type:

    1. mpm_worker will be both multi-process and multi-thread, with the number of processes dictated by the ServerLimit variable.
    2. prefork will be multi-process
    3. perchild will be multi-process as well

    Edit: To answer your second edited question:

    It's quite easy. Store it in a file:

    function readStatus() {
        $f = fopen('/path/to/myfile', 'r');
        if (!$f) return false;
        if (flock($f, LOCK_SH)) {
            $ret = fread($f, 8192);
            flock($f, LOCK_UN);
            fclose($f);
            return $ret;
        }
        fclose($f);
        return false;
    }
    
    function updateStatus($new) {
        $f = fopen('/path/to/myfile', 'w');
        if (!$f) return false;
        if (flock($f, LOCK_EX)) {
            ftruncate($f, 0);
            fwrite($f, $new);
            flock($f, LOCK_UN);
            fclose($f);
            return true;
        }
        fclose($f);
        return false;
    }
    
    function incrementStatus() {
        $f = fopen('/path/to/myfile', 'rw');
        if (!$f) return false;
        if (flock($f, LOCK_EX)) {
            $current = fread($f, 8192);
            $current++;
            ftruncate($f, 0);
            fwrite($f, $current);
            flock($f, LOCK_UN);
            fclose($f);
            return true;
        }
        fclose($f);
        return false;
    }
    
    0 讨论(0)
  • 2020-11-28 10:08

    I recently created my own simple implementation of a mutex-like mechanism using the flock function of PHP. Of course the code below can be improved, but it is working for most use cases.

    function mutex_lock($id, $wait=10)
    {
      $resource = fopen(storage_path("app/".$id.".lck"),"w");
    
      $lock = false;
      for($i = 0; $i < $wait && !($lock = flock($resource,LOCK_EX|LOCK_NB)); $i++)
      {
        sleep(1);
      }
    
      if(!$lock)
      {
        trigger_error("Not able to create a lock in $wait seconds");
      }
    
      return $resource;
    }
    
    function mutex_unlock($id, $resource)
    {
      $result = flock($resource,LOCK_UN);
      fclose($resource);
    
      @unlink(storage_path("app/".$id.".lck"));
    
      return $result;
    }
    
    0 讨论(0)
  • 2020-11-28 10:11

    Yes that's true, as PHP is run by Apache, and Apache can organize the threads of execution as it deems the best (see the various worker model). So if you want to access a resource one at a time, you either lock to a file (which is good if you are dealing with cron jobs for example), or you rely on database transaction mechanism, ACID features, and database resources locking, if you are dealing with data.

    0 讨论(0)
提交回复
热议问题