略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: GearmanWorker::addServer

2024-12-22

GearmanWorker::addServer

(PECL gearman >= 0.5.0)

GearmanWorker::addServerAdd a job server

说明

public GearmanWorker::addServer(string $host = 127.0.0.1, int $port = 4730): bool

Adds a job server to this worker. This goes into a list of servers than can be used to run jobs. No socket I/O happens here.

参数

host

任务服务器主机名。

port

任务服务器端口号。

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 Add alternate Gearman servers

<?php
$worker
= new GearmanWorker(); 
$worker->addServer("10.0.0.1"); 
$worker->addServer("10.0.0.2"7003);
?>

参见

add a noteadd a note

User Contributed Notes 7 notes

up
5
magge
9 years ago
If you suddenly start getting a:

PHP Fatal error:  Uncaught exception 'GearmanException' with message 'Failed to set exception option' in

...on your GearmanWorker::work() calls, I was able to fix this by specifying values to GearmanWorker::addServer(), even if they are the same as the documented default values.

Crashes:

<?php
$gmw
= new GearmanWorker();
$gmw->addServer();
$gmw->work();
?>

Works:

<?php
$gmw
= new GearmanWorker();
$gmw->addServer("127.0.0.1", 4730);
$gmw->work();
?>

Go figure. :)
up
2
liv_romania at yahoo dot com
6 years ago
To properly test the server added you could use the following code:

<?php
// create the worker
$worker= new GearmanWorker();

// add the  job server (bad host/port)
$worker->addServer('127.0.0.2', 4731);

// define a variable to hold application data
$count = 0;

// add the reverse function
$worker->addFunction('reverse', 'my_reverse_function', $count);

// test job server response
if (!@$worker->echo('test data')) {
    die(
$worker->error());
}

// start the worker listening for job submissions
while ($worker->work());

function
my_reverse_function($job, &$count)
{
   
$count++;

    return
$count . ': ' . strrev($job->workload()) . "\n";
}
?>
up
1
anubhav dot jha at gmail dot com
5 years ago
I was receving following message on add server Uncaught exception 'GearmanException' with message 'Failed to set exception option' in <<filename>>:<<linenumber >>
was on centos 6.5

fixed by following steps:
yum install gearmand
/etc/init.d/gearmand start
up
1
mike at eastghost dot com
5 years ago
PHP Fatal error:  Uncaught exception 'GearmanException' with message 'Failed to set exception option'

also indicates the gearmand daemon is not running.
up
1
gabe dot spradlin at gmail dot com
8 years ago
The manual states that you get a TRUE on success and FALSE on failure. When I have attempted to connect to a server that is powered off I still get TRUE. The return from returnCode() is 0 which is the same as the returnCode() from a successful connection.

I have not yet found a way around this.
up
-2
617137379 at qq dot com
8 years ago
No socket I/O happens in addserver.
up
-1
allevo
5 years ago
On Ubuntu (php7 php-gearman/xenial,now 2.0.2+1.1.2-1+deb.sury.org~xenial+1 amd64) this function throws a GearmanException.
Please use in try catch

官方地址:https://www.php.net/manual/en/gearmanworker.addserver.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3