[リソース]PHP使用メッセージキュー

8904 ワード

PHP操作Linuxメッセージキューによるプロセス間通信の完了
HTTPプロトコルに基づく軽量レベルのオープンソース単純キューサービス:HTTPSQS[オリジナル]
Redisキュー-PHP操作の簡単な例
    
<?php
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
while(True){
  try{
    $value = 'value_'.date('Y-m-d H:i:s');
    $redis->LPUSH('key1',$value);
    sleep(rand()%3);
    echo $value."
"; }catch(Exception $e){ echo $e->getMessage()."
"; } } ?> <?php $redis = new Redis(); $redis->pconnect('127.0.0.1',6379); while(True){ try{ echo $redis->LPOP('key1')."
"; }catch(Exception $e){ echo $e->getMessage()."
"; } sleep(rand()%3); } ?>

redisのキューメカニズムを利用してphpのマルチスレッドを実現する
プログラミングにおけるキューの実際の応用(php)
phpのタスクキュー-shmop
<?php   
/**  
*        PHP          
*      ,              
*  :          ,    unset(),         
*  
* @author [email protected]  
* @created 2009-12-23   
*/  
class SHMQueue   
{   
    private $maxQSize = 0; //          
       
    private $front = 0; //        
    private $rear = 0;  //        
       
    private $blockSize = 256;  //     (byte)   
    private $memSize = 25600;  //       (byte)   
    private $shmId = 0;   
       
    private $filePtr = './shmq.ptr';   
       
    private $semId = 0;   
    public function __construct()   
    {          
        $shmkey = ftok(__FILE__, 't');   
           
        $this->shmId = shmop_open($shmkey, "c", 0644, $this->memSize );   
        $this->maxQSize = $this->memSize / $this->blockSize;   
           
         //           
        $this->semId = sem_get($shmkey, 1);   
        sem_acquire($this->semId); //                   
           
        $this->init();   
    }   
       
    private function init()   
    {   
        if ( file_exists($this->filePtr) ){   
            $contents = file_get_contents($this->filePtr);   
            $data = explode( '|', $contents );   
            if ( isset($data[0]) && isset($data[1])){   
                $this->front = (int)$data[0];   
                $this->rear  = (int)$data[1];   
            }   
        }   
    }   
       
    public function getLength()   
    {   
        return (($this->rear - $this->front + $this->memSize) % ($this->memSize) )/$this->blockSize;   
    }   
       
    public function enQueue( $value )   
    {   
        if ( $this->ptrInc($this->rear) == $this->front ){ //      
            return false;   
        }   
           
        $data = $this->encode($value);   
        shmop_write($this->shmId, $data, $this->rear );   
        $this->rear = $this->ptrInc($this->rear);   
        return true;   
    }   
           
    public function deQueue()   
    {   
        if ( $this->front == $this->rear ){ //      
            return false;   
        }   
        $value = shmop_read($this->shmId, $this->front, $this->blockSize-1);   
        $this->front = $this->ptrInc($this->front);   
        return $this->decode($value);   
    }   
       
    private function ptrInc( $ptr )   
    {   
        return ($ptr + $this->blockSize) % ($this->memSize);   
    }   
       
    private function encode( $value )   
    {   
        $data = serialize($value) . "__eof";   
        if ( strlen($data) > $this->blockSize -1 ){   
            throw new Exception(strlen($data)." is overload block size!");   
        }   
        return $data;   
    }   
       
    private function decode( $value )   
    {   
        $data = explode("__eof", $value);   
        return unserialize($data[0]);          
    }   
       
    public function __destruct()   
    {   
        $data = $this->front . '|' . $this->rear;   
        file_put_contents($this->filePtr, $data);   
           
        sem_release($this->semId); //     ,         
    }   
}  


         :
//     
$shmq = new SHMQueue();   
$data = 'test data';   
$shmq->enQueue($data);   
unset($shmq);   
//        
$shmq = new SHMQueue();   
$data = $shmq->deQueue();   
unset($shmq);

PHP実現キュー及びキュー原理
キューはシーンを適用し、自分でキューを実装します.
RabbitMQ
php mqtt
PHP大手サイトのアーキテクチャの実例分析
fastcgi_の使用finish_requestページ応答速度の向上
Redis pub/sub(Publish,Subscribe)