Working & waiting in php ext 0.6

15 views
Skip to first unread message

Denis Arh

unread,
Oct 15, 2009, 1:04:07 PM10/15/09
to gearman
Hi,

I just can not get this working... must be something silly...

Standard client code:
$c = new GearmanClient();
$c->addServer()
$c->do('funct', 'some data');

Standard worker code:
$w = new GearmanWorker();
$w->addServer();
$w->addFunction('funct', 'funct');
$w->work()


How to include non-blocking option & wait() method?

$w->addOptions(GEARMAN_WORKER_NON_BLOCKING);
do {
$w->work();
$w->wait();
usleep(500000);
} while(true);


$w->addOptions(GEARMAN_WORKER_NON_BLOCKING);
$w->work();
do {
$w->wait();
usleep(500000);
} while(true);


Any suggestions? None of this works... if I put non-blocking option
worker does not "grab" jobs from the server...
I could do this with setTimeout() and calling work() in a loop but
somehow this does not feel right.


Eric Day

unread,
Oct 16, 2009, 1:14:01 PM10/16/09
to gea...@googlegroups.com
Hi Denis,

Well, it all depends on why you want to use non-blocking mode.Do you
have other file descriptors you want to listen on? Do you just want
the work() method to return for some type of check periodically?

I think most folks will just want to use the timeout:

$worker= new GearmanWorker();
$worker->setTimeout(5000);
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('reverse', 'my_reverse_fn');
while (@$worker->work() || $worker->returnCode() == GEARMAN_TIMEOUT)
print "Waiting for next job...\n";
print "Worker Error: " . $worker->error() . "\n";

This will allow you do some type after each job or every 5 seconds.

For non-blocking, the only use case I find useful right now is in the
client to send all requests but not block on them yet. I'm thinking
we'll be adding a new API call to just "sendTasks" and not actually
wait for results. I don't see much use in NON_BLOCKING for the worker
(unless you have other FDs outside of Gearman you need to manage
concurrently).


$worker= new GearmanWorker();
$worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('reverse', 'my_reverse_fn');
while (@$worker->work() ||
$worker->returnCode() == GEARMAN_IO_WAIT ||
$worker->returnCode() == GEARMAN_NO_JOBS)
{
if ($worker->returnCode() == GEARMAN_SUCCESS)
continue;

print "Waiting for next job...\n";
if (!@$worker->wait())
{
if ($worker->returnCode() == GEARMAN_NO_ACTIVE_FDS)
{
# We are not connected to any servers, so wait a bit before
# trying to reconnect.
sleep(5);
continue;
}

break;
}
}
print "Worker Error: " . $worker->error() . "\n";


As for your code, this one makes more sense than the other, but you
don't need a 'usleep' in there. The wait() method blocks on poll(),
and acts as your "sleep until file I/O".

> $w->addOptions(GEARMAN_WORKER_NON_BLOCKING);
> do {
> $w->work();
> $w->wait();
> usleep(500000);
> } while(true);

Hope this helps. :)

-Eric

Denis Arh

unread,
Oct 17, 2009, 11:29:53 PM10/17/09
to gearman
Thank you Eric for the code samples.

>
> As for your code, this one makes more sense than the other, but you
> don't need a 'usleep' in there. The wait() method blocks on poll(),
> and acts as your "sleep until file I/O".

I'm using gearmain in one of my app that uses forks to initialize
multiple workers that can then do some work in parallel. And if I use
wait() and "sleep util file I/O" PHP will not allow me to catch any
signals (like KILL).

So, in this case my only option is to use setTimeout() and work().

Regards,
Denis
Reply all
Reply to author
Forward
0 new messages