加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何构建PHP队列系统

发布时间:2020-12-13 13:27:32 所属栏目:PHP教程 来源:网络整理
导读:我不得不构建一个 PHP队列系统,并找到了这篇精彩的文章 http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project/和我用它来创建一个PHP队列系统,它非常容易设置和使用. 下面是queue.php的代码,从
我不得不构建一个 PHP队列系统,并找到了这篇精彩的文章
http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project/和我用它来创建一个PHP队列系统,它非常容易设置和使用.

下面是queue.php的代码,从shell运行(puTTy或somesuch).

<?PHP 

//. set this constant to false if we ever need to debug
//. the application in a terminal.
define('QUEUESERVER_FORK',true);

//////// fork into a background process ////////
if(QUEUESERVER_FORK){    
    $pid = pcntl_fork(); 
    if($pid === -1) die('error: unable to fork.');    
    else if($pid) exit(0);        
    posix_setsid();    
    sleep(1);        
    ob_start();
}

$queue = array();

//////// setup our named pipe ////////
$pipefile = '/tmp/queueserver-input';

if(file_exists($pipefile))    
    if(!unlink($pipefile))         
        die('unable to remove stale file');

umask(0);


if(!posix_mkfifo($pipefile,0666))    
    die('unable to create named pipe');

$pipe = fopen($pipefile,'r+');

if(!$pipe) die('unable to open the named pipe');

stream_set_blocking($pipe,false);

//////// process the queue ////////
while(1){    

    while($input = trim(fgets($pipe))){        
        stream_set_blocking($pipe,false);        
        $queue[] = $input;    
    }    

    $job = current($queue);    
    $jobkey = key($queue);    

    if($job){        
        echo 'processing job ',$job,PHP_EOL;                
        process($job);                
        next($queue);        
        unset($job,$queue[$jobkey]);            
    }else{        
        echo 'no jobs to do - waiting...',PHP_EOL;        
        stream_set_blocking($pipe,true);    
    }        

    if(QUEUESERVER_FORK) ob_clean();

}

?>

最困难的部分是让pcntl功能在我的服务器上运行.

我的问题是“如果服务器必须重新启动,我如何才能自动启动作业?”

My question is “How do i get the job to start automatically when/if the server has to restart?”

通过将其添加到服务器启动时启动的事物列表中.不幸的是,这样做的说明因操作系统和操作系统版本而异.您可能希望使用更多跨平台的东西.我有很多运气与supervisor,你可以在你选择的操作系统的包回购中找到.

那就是说,你正走在疯狂的路上.你正在做的事情之前已经完成了,更好的是,由令人敬畏的人.查看Gearman工作队列系统和随附的PECL extension.发生这样的情况,主管也非常方便让您的Gearman工作人员保持活力.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读