문제

There is no dearth of examples showing how to use Gearman from PHP out there. A typical one would go like this

**CLIENT**

<?php
  $client= new GearmanClient();
  $client->addServer();
  print $client->do("revit", "AlL THE World's a sTagE");
  print "\n";
?>

**SERVER**

<?php
   $worker= new GearmanWorker();
   $worker->addServer();
   $worker->addFunction("revit", "rev_it");
   while ($worker->work());

   function rev_it($job)
   {
      return strrev($job->workload());
   }
?>

I installed Gearmand and the PECL PHP extension on my 64 bit CentOS server, wrote up these scripts, ensured that Gearmand was running and then browsed to the client. The browser waited... .

I had fully expected this since I figured that the Gearman server needs to somehow know that it should execute that particular worker script when it receives an appropriate client request.

I opened another tab and browsed to the worker script and promptly got back a response in the client script tab.

There appears to be a missing link here. When one writes a worker script does it not somehow have to be registered with the Gearman server so the latter knows to use it to service certain clients?

Either I have been Googling for the wrong things or else all those "How to use Gearman in PHP" examples leave out something. Could someone here help?

도움이 되었습니까?

해결책

You're supposed to run the worker script from the console (or under something like GearmanManager or supervisord (or screen)), where it'll connect to gearmand, register it function and wait for work to be handed over to it from gearmand.

The flow of execution would be something like this:

Start a worker in a console terminal

GearmanWorker -> register function              -> gearmand

A web request arrives

browser       -> request page                   -> GearmanClient
GearmanClient -> perform this function          -> gearmand
gearmand      -> here, do this function         -> GearmanWorker
GearmanWorker -> here's the result              -> gearmand
gearmand      -> we're done, this is the result -> GearmanClient
GearmanClient -> return data to executing php   -> browser

Hopefully that explains a bit more how things are interconnected, and why you didn't get a result the first time if you didn't have the worker actually running somewhere and registered with gearmand when the client were executed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top