예제 #1
0
<?php

# The client script
# create our gearman client
$gmc = new GearmanClient();
# add the default job server
$gmc->addServer();
# set a couple of callbacks so we can track progress
$gmc->setCompleteCallback("reverse_complete");
$gmc->setStatusCallback("reverse_status");
# add a task for the "reverse" function
$task = $gmc->addTask("reverse", "Hello World!", null, "1");
# add another task, but this one to run in the background
$task = $gmc->addTaskBackground("reverse", "!dlroW olleH", null, "2");
if (!$gmc->runTasks()) {
    echo "ERROR " . $gmc->error() . "\n";
    exit;
}
echo "DONE\n";
function reverse_status($task)
{
    echo "STATUS: " . $task->unique() . ", " . $task->jobHandle() . " - " . $task->taskNumerator() . "/" . $task->taskDenominator() . "\n";
}
function reverse_complete($task)
{
    echo "COMPLETE: " . $task->unique() . ", " . $task->data() . "\n";
}
?>
 
예제 #2
0
 public function addTaskBackground($name, $params = '', &$context = null, $unique = null)
 {
     $method = self::GEARMAN_METHOD_ADDTASKBACKGROUND;
     $params = serialize($params);
     $contextReference = array('context' => &$context);
     $unique = $this->generateUniqueKey($name, $params, $unique, $method);
     $this->gearmanClient->addTaskBackground($name, $params, $contextReference, $unique);
     return $unique;
 }
예제 #3
0
 public function addTaskBackground($function_name, $workload, &$context = null, $unique = null)
 {
     $function_name = $this->_processFunctionName($function_name);
     return parent::addTaskBackground($function_name, $workload, $context, $unique);
 }
예제 #4
0
<?php

$client = new GearmanClient();
$client->addServer('127.0.0.1', 4730);
//本机可以直接addServer(),默认服务器端使用4730端口
$client->setCompleteCallback('completeCallBack');
//先绑定才有效
$result1 = $client->do('say', 'do');
//do是同步进行,进行处理并返回处理结果。
$result2 = $client->doBackground('say', 'doBackground');
//异步进行,只返回处理句柄。
$result3 = $client->addTask('say', 'addTask');
//添加任务到队列,同步进行?通过添加task可以设置回调函数。
$result4 = $client->addTaskBackground('say', 'addTaskBackground');
//添加后台任务到队列,异步进行?
$client->runTasks();
//运行队列中的任务,只是do系列不需要runTask()。
echo 'result1:';
var_dump($result1);
echo '<br/>';
echo 'result2:';
var_dump($result2);
echo '<br/>';
echo 'result3:';
var_dump($result3);
echo '<br/>';
echo 'result4:';
var_dump($result4);
echo '<br/>';
//绑定回调函数,只对addTask有效
function completeCallBack($task)
예제 #5
0
<?php

$client = new GearmanClient();
$client->addServer();
$client->setCompleteCallback('completeCallBack');
for ($i = 1; $i <= 1; $i++) {
    $client->addTaskBackground('pm', 'pm instances' . $i);
}
for ($i = 1; $i <= 1; $i++) {
    $client->addTaskBackground('sm', 'sm instances' . $i);
}
$t1 = microtime(true);
$client->runTasks();
//运行队列中的任务,只是do系列不需要runTask()。
echo '耗时' . round(microtime(true) - $t1, 3) . '秒' . PHP_EOL;
//绑定回调函数,只对addTask有效
function completeCallBack($task)
{
    echo 'result:' . print_r($task->data(), 1) . PHP_EOL;
}
예제 #6
0
$client = new GearmanClient();
$client->addServer(GEARMAN_SERVER_IP);
if (($socket = socket_create(AF_INET, SOCK_STREAM, 0)) < 0) {
    echo "failed to create socket: " . socket_strerror($socket) . "\n";
    exit($socket);
}
if (($ret = socket_bind($socket, GEARMAN_SERVER_IP, GEARMAN_SERVER_PORT)) < 0) {
    echo "failed to bind socket: " . socket_strerror($ret) . "\n";
    exit(abs($ret));
}
if (($ret = socket_listen($socket, 0)) < 0) {
    echo "failed to listen to socket: " . socket_strerror($ret) . "\n";
    exit(abs($ret));
}
socket_set_nonblock($socket);
$task = $client->addTaskBackground('atoum', 'atoum');
$client->runTasks();
echo "waiting for clients to connect\n";
while (true) {
    $connection = @socket_accept($socket);
    if ($connection === false) {
        usleep(100);
    } elseif ($connection > 0) {
        $pid = pcntl_fork();
        if ($pid === -1) {
            echo "fork failure!\n";
            break;
        } elseif ($pid === 0) {
            socket_close($socket);
            $buff = '';
            socket_recv($connection, $buff, 65536, MSG_EOF);