启动和停止Gearman PHP中的工人 [英] Start and stopping workers in gearman php

查看:146
本文介绍了启动和停止Gearman PHP中的工人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功配置并使用了gearman及其pecl php扩展名.我正在使用它在后台执行有关长sql查询的长过程.我正在使用Yii btw,如果该细节有帮助的话.

I've successfully configured and used gearman and its pecl php extension. I'm using it to execute a long process concerning long sql queries in a background. I'm using Yii btw, if that detail helps.

这是我的使用方式:

public function actionProcessWithGearman(){

$output = shell_exec('gnome-terminal -e "php workers/worker.php" > /dev/null 2>/dev/null &');


$client = new GearmanClient();
$client->addServer();
$result = $client->doBackground('executeJob',//parameters);

}

一些详细信息:

如果您注意到我先运行 gnome-terminal ,这样我就可以看到该过程,而不是直接使用 php命令 ,我还添加了 /dev/null ,以便它不再等待响应.然后,工人被唤醒并开始工作.

If you notice I run a gnome-terminal first, so that I can see the process rather than going directly with the php command, I also added /dev/null so that it will no longer wait for a response. And then the worker is woken up and runs the job.

问题:

我的问题出在以下情况:执行了多次该操作或由不同客户端中的多个用户执行,结果是实例化了运行 worker.php 的多个终端.

My problem arises when this action is executed several times or executed by several users in different clients, and as a result, multiple terminals running worker.php are being instantiated.

我怎么只有一个工作人员?,即使我可以在不同客户端中为多个用户提供多个工作人员,每次任务完成时如何关闭工作人员?

How do I have only one worker? and even if I can have several workers for several users in the different clients, how do I close a worker everytime the task is finished?

推荐答案

如果您有适当的队列系统,则工作人员应始终在后台运行,并准备抓捕新工作.

If you have a queue system in place the workers should always be running in the background and be ready to catch new jobs.

我认为对队列系统逻辑有误解.您应该仅添加将由工作人员处理的新作业,如果要完成工作,则不要启动工作人员.

I think there is a misunderstanding on the queue system logic. You should just add a new job that will be processed by the worker, and not start the worker if you want a job to be done.

齿轮工通常与主管相处得很好.主管将控制工作人员并确保其始终可用.如果您需要同时运行多个作业,则可以随时添加一个新工作器.

Gearman usually goes well with supervisor. Supervisor will control the worker and make sure it will be always available. If you need more than one job to be running at the same time then you can always add a new worker.

只需寻找Gearman + Supervisor + PHP,就会有很多文章介绍如何进行设置.

Just look for gearman + supervisor + php there are a lot of articles explaining how to set that up.

这篇关于启动和停止Gearman PHP中的工人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆