Php 调用doBackground时,如何注册函数和gearmanworker?
客户端:Php 调用doBackground时,如何注册函数和gearmanworker?,php,gearman,Php,Gearman,客户端: <?php $gmclient= new GearmanClient(); $gmclient->addServer(); $gmclient->doBackground("reverse", "this is a test"); echo "done!\n"; <?php echo "Starting\n"; $gmworker= new GearmanWorker(); $gmworker->addServer(); $gmworker-&g
<?php
$gmclient= new GearmanClient();
$gmclient->addServer();
$gmclient->doBackground("reverse", "this is a test");
echo "done!\n";
<?php
echo "Starting\n";
$gmworker= new GearmanWorker();
$gmworker->addServer();
$gmworker->addFunction("reverse", "reverse_fn");
while($gmworker->work()){}
function reverse_fn($job)
{
$result= strrev($job->workload());
$fp = fopen('test.txt', 'a');
fwrite($fp, $result);
fclose($fp);
echo "Result: $result\n";
return $result;
}
?>
然后它写入文件,但shell只是停留在
[user@server gearman]$ php worker.php
Starting
Result: tset a si siht
我是否可以让它在调用doBackground时在后台运行worker.php文件?我还想确保gearmand重新启动时这项工作正常,这样,如果守护进程重新启动,我就不必运行worker.php。作为我评论的答案发布:
请看一下管理php脚本的supervisord()。supervisor将为您进行流程管理,如果php脚本死掉,将在后台重新启动它们查看supervisor()以管理您的php脚本。主管将为您进行流程管理,如果php脚本死亡,将在后台重新启动它们。@Sachin:您应该将您的评论作为答案发布。我只见过PHP脚本与gearman交互,作为守护进程运行。尽管我们使用专有的过程控制管理系统,supervisord似乎非常适合管理子过程。@Sachin我就是这么做的。谢谢
[user@server gearman]$ php worker.php
Starting
Result: tset a si siht