Gearman with PHP - 工作线程

发布于 2024-11-17 20:05:02 字数 1127 浏览 3 评论 0原文

我对 Gearman 和 PHP 的工作人员有疑问。 我想同时运行相同的功能。但现在 Gearman 似乎让它成为了一个队列。

我正在搜索的输出是:

$ ./daemon.php  
Starting daemon...
Received job: H:www-dev1:15 
Received job: H:www-dev1:16 
Finished
Finished

但当前代码的输出是:

$ ./daemon.php  
Starting daemon...
Received job: H:www-dev1:15 
Finished
Received job: H:www-dev1:16 
Finished

Is there possible to fork GearmanWorker using pcntl_fork()?

客户:

$client = new GearmanClient();
$client->addServer();

$args = array('test' => 'test1');
$args = serialize($args);

$client->doBackground('test', $args);
sleep(1);
$client->doBackground('test', $args);

echo "Done";

工人:

#!/usr/bin/php
<?php

echo "Starting daemon..." . PHP_EOL;

$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('test', 'testFunc');

while ($worker->work()) {
}

function testFunc($job) {

    echo "Received job: " . $job->handle() . PHP_EOL;
    sleep(10);
    echo "Finished" . PHP_EOL;  
}

I have a problem with Gearman and the worker for PHP.
I want to run the same function at the same time. But now Gearman seems to make it a queue.

The output I'm searching for is:

$ ./daemon.php  
Starting daemon...
Received job: H:www-dev1:15 
Received job: H:www-dev1:16 
Finished
Finished

But the output of current code is:

$ ./daemon.php  
Starting daemon...
Received job: H:www-dev1:15 
Finished
Received job: H:www-dev1:16 
Finished

Is there possible to fork GearmanWorker using pcntl_fork()?

Client:

$client = new GearmanClient();
$client->addServer();

$args = array('test' => 'test1');
$args = serialize($args);

$client->doBackground('test', $args);
sleep(1);
$client->doBackground('test', $args);

echo "Done";

Worker:

#!/usr/bin/php
<?php

echo "Starting daemon..." . PHP_EOL;

$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('test', 'testFunc');

while ($worker->work()) {
}

function testFunc($job) {

    echo "Received job: " . $job->handle() . PHP_EOL;
    sleep(10);
    echo "Finished" . PHP_EOL;  
}

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

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

发布评论

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

评论(2

怪我鬧 2024-11-24 20:05:02

一名工人可以同时处理一项工作。如果你需要执行多个作业,当然你需要创建多个工人。在您的情况下,只需多次执行“Worker”脚本并将它们放在后台即可。或者(正如您自己提到的)创建分叉,但第一个肯定更容易;)

One worker can handle one job at one time. If you need to execute more than one job, you need to create more than one worker of course. in your case just execute the "Worker"-script multiple times and put them in the background. Or (as you mentioned yourself) create forks, but the first one is definetly easier ;)

述情 2024-11-24 20:05:02

您可以尝试worker克隆功能来创建更多worker

GearmanWorker::clone(void)

you can try worker clone function to create more workers

GearmanWorker::clone(void)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文