php多个进程写入一个文件与模拟一个队列
来源:互联网 发布:苹果软件开发培训班 编辑:程序博客网 时间:2024/05/16 04:34
function write_file($filename, $content){ $lock = $filename . '.lck'; $write_length = 0; while(true) { if( file_exists($lock) ) { usleep(100); } else { touch($lock); $write_length = file_put_contents($filename, $content, FILE_APPEND); break; } } if( file_exists($lock) ) { unlink($lock); } return $write_length;}
class DEQueue { //存储 protected $_storage = array(); //入头 public function unshift($element) { return array_unshift($this->_storage, $element); } //入尾 public function push($element) { return array_push($this->_storage, $element); } //出尾 public function pop() { return array_pop($this->_storage); } //出头 public function shift() { return array_shift($this->_storage); } //长度 public function length() { return count($this->_storage); }}