How to Build a PHP Queue System(如何构建 PHP 队列系统)
问题描述
我必须构建一个 PHP 队列系统,发现这个 精彩文章 我用过它创建了一个PHP队列系统,非常容易设置和使用.http://squirrelshaterobots.com/programming/php/building-a-queue-server-in-php-part-1-understanding-the-project
I had to build a PHP Queue System, and found this brilliant article
and 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
下面是 queue.php 的代码,从 shell(puTTy 或类似的)运行.
Below is the code for queue.php, run from shell (puTTy or 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 函数在我的服务器上运行.
The hardest part was getting the pcntl functions to work on my server.
我的问题是当/如果服务器必须重新启动时,我如何让作业自动启动?"
My question is "How do i get the job to start automatically when/if the server has to restart?"
<小时>正如评论中所指出的,编辑了断开的链接,并为后代指出了优秀的网络存档.
As noted in comments, edited broken link and pointed to excellent web archive for posterity.
推荐答案
我的问题是当/如果服务器必须重新启动时,我如何让作业自动启动?"
My question is "How do i get the job to start automatically when/if the server has to restart?"
通过将其添加到服务器启动时启动的事物列表中.不幸的是,这样做的说明因操作系统和操作系统版本而异.您可能想要使用稍微跨平台的东西.supervisor 让我非常幸运,您可能可以在您选择的操作系统的软件包存储库中找到它.
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.
也就是说,你正走在疯狂的道路上.你正在做的事情以前已经做过了,更好的是,很棒的人.查看 Gearman 工作队列系统和配套的 PECL 扩展.碰巧,主管也很方便让您的 Gearman 工人活着.
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 Gearman work queue system and the accompnaying PECL extension. It happens that supervisor is pretty handy for keeping your Gearman workers alive as well.
这篇关于如何构建 PHP 队列系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何构建 PHP 队列系统
基础教程推荐
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01