如何构建 PHP 队列系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14149291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Build a PHP Queue System
提问by Folding Circles
I had to build a PHP Queue System, and found this brilliant articleand I used it to create a PHP queue system, its very easy to set-up and use.http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project
我不得不构建一个 PHP 队列系统,并找到了这篇精彩的文章,我用它来创建一个 PHP 队列系统,它非常易于设置和使用。http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project
Below is the code for queue.php, run from shell (puTTy or somesuch).
下面是 queue.php 的代码,从 shell(puTTy 或类似的东西)运行。
<?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();
}
?>
The hardest part was getting the pcntl functions to work on my server.
最难的部分是让 pcntl 函数在我的服务器上工作。
My question is "How do i get the job to start automatically when/if the server has to restart?"
我的问题是“当/如果服务器必须重新启动时,我如何让工作自动启动?”
正如评论中所指出的,编辑了断开的链接,并为后代指出了优秀的网络档案。
采纳答案by Charles
My question is "How do i get the job to start automatically when/if the server has to restart?"
我的问题是“当/如果服务器必须重新启动时,我如何让工作自动启动?”
By adding it to the list of things started when the server starts. Unfortunately the instructions for doing so vary wildly by operating system and OS version. You probably want to use something slightly more cross-platform. I've had a great deal of luck with supervisor, which you can probably find in the package repos on your OS of choice.
通过将其添加到服务器启动时启动的事物列表中。不幸的是,这样做的说明因操作系统和操作系统版本而异。您可能想要使用稍微跨平台的东西。我很幸运有supervisor,您可能可以在您选择的操作系统的软件包存储库中找到它。
That said, you are going down the route of madness. The thing you're doing has been done before, better, by awesome people. Check out the Gearmanwork queue system and the accompnaying PECL extension. It happens that supervisor is pretty handy for keeping your Gearman workers alive as well.
也就是说,你正在走疯狂的道路。你正在做的事情以前做过,更好,由很棒的人完成。查看Gearman工作队列系统和随附的PECL 扩展。碰巧主管对于让您的 Gearman 工人保持活力也非常方便。

