编程语言
首页 > 编程语言> > 与PHP的Gearman – 在工作者的螺纹

与PHP的Gearman – 在工作者的螺纹

作者:互联网

我有一个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

是否可以使用pcntl_fork()来分叉GearmanWorker?

客户:
    

$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;  
}

解决方法:

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

标签:php,multithreading,fork,gearman
来源: https://codeday.me/bug/20190626/1295352.html