如何将 GearmanJob 与 GearmanWorker 和 GearmanClient 联系起来?

发布于 2024-11-15 13:20:30 字数 330 浏览 2 评论 0原文

public GearmanTask GearmanClient::addTask  ( string $function_name  , string $workload  [, mixed &$context  [, string $unique  ]] )
public bool GearmanWorker::addFunction  ( string $function_name  , callback $function  [, mixed &$context  [, int $timeout  ]] )

这些是可用于集成两者的类方法。 有了这些,您如何将工作负载与被调用的函数联系起来?

public GearmanTask GearmanClient::addTask  ( string $function_name  , string $workload  [, mixed &$context  [, string $unique  ]] )
public bool GearmanWorker::addFunction  ( string $function_name  , callback $function  [, mixed &$context  [, int $timeout  ]] )

These are class methods that can be used to integrate the two.
With these, how do you relate the workload with the called function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2024-11-22 13:20:30

GearmanClient 用于提交任务。通常,这是通过网页或用于读取要提交的任务列表的脚本来完成的。

GearmanWorker 的设置方式是让许多并行“工作线程”可以同时运行。从逻辑上讲,工人所做的任何事情都应该代表单个原子工作单元。这可能意味着将一个对象转换为另一个对象并将其保存回数据库,或者为单个用户组装并发送 HTML 电子邮件。 $function_name 是一个采用单个参数的函数,该参数是一个 GearmanJob 对象。

因此,您的控制器脚本可能如下所示。

 $gearman_params = json_encode( 数组(
      'id'=> 77、
      '选项' =>数组('推' => true),
  ) );
  $client = new GearmanClient();
  $client->doBackground( "widgetize", $gearman_params );

然后,你的工人会做这样的事情。

$gmworker  = GearmanWorker;
$gmworker->addFunction( "widgetize", "widgetize" );
while( $gmworker->work() ) {  

if ( $gmworker->returnCode() !== GEARMAN_SUCCESS ) {
    echo "Worker Terminated." . PHP_EOL ;
    break;
  }

} // while *working*

function widgetize( $job ) {
    $workload = json_decode( $job->workload() );

    /* do stuff */
}

需要记住的一些事情:

  • 工作脚本被设计为长时间运行的脚本,因此需要 while 循环。确保正确设置超时和内存限制。
  • 给工人一个运行限制也是一个好习惯。例如,只允许工作程序运行几次,然后exit; PHP 的内存使用情况仍然很糟糕,并且很难判断工作程序何时会因为内存不足而被杀死。
  • 同样,最好将工作人员设计为幂等的,这样,如果工作人员失败,可以将工作重新提交给工作人员。
  • 客户端可以向工作人员提交具有不同优先级的作业,并且它们不一定必须在后台运行,如上面的示例。它们可以运行以便客户端阻塞。

GearmanClient is used to submit a task. Normally this is done from a web page, or a script meant to read a list of tasks to be submitted.

GearmanWorker is meant to be set up in such a way that many parallel 'workers' can be run at the same time. Logically, whatever a worker does should represent a single atomic unit of work. That can mean doing a single transformation of an object into another and saving it back to the database, or assembling and sending an html email for a single user. $function_name is a function that takes a single argument, which is a GearmanJob object.

So, your controller script might look something like this.

  $gearman_params = json_encode( array(
      'id'       => 77,
      'options'  => array('push' => true),
  ) );
  $client = new GearmanClient();
  $client->doBackground( "widgetize", $gearman_params );

Then, your worker will do something like this.

$gmworker  = GearmanWorker;
$gmworker->addFunction( "widgetize", "widgetize" );
while( $gmworker->work() ) {  

if ( $gmworker->returnCode() !== GEARMAN_SUCCESS ) {
    echo "Worker Terminated." . PHP_EOL ;
    break;
  }

} // while *working*

function widgetize( $job ) {
    $workload = json_decode( $job->workload() );

    /* do stuff */
}

A few things to keep in mind:

  • The worker script is designed to be long running script, hence the while loop. Make sure to set your timeout and memory limits appropriately.
  • It's also good practice to give the worker a run limit. As in, only allow the worker to run a few times, and then exit; PHP still sucks with memory usage, and it can be hard to tell when a worker will be killed because it ran out of memory.
  • In the same vein it's also best to design workers to be idempotent, so jobs can be resubmitted to workers if they fail.
  • The Client can submit jobs to the worker with different priorities, and they don't neccessarily have to be run in the background, like the above example. They can be run so that the client blocks.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文