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