Follow this English post to learn
Backup of original source code
Example 1:
Like we've shown the basic architecture of Net_geaman, the client-side main code:
// 一个client对象,需要jobserver信息,一个数组$clientnew Net_Gearman_Client (gm::$servers);// Example1就是function name$client->Example1 (array ('date'));
Worker-side main code:
// worker对象,需要jobserver信息$workernew Net_Gearman_Worker (gm::$servers);// 传入function name,给他处理Example1的能力$worker->addAbility ('Example1');// loop$worker->beginWork ();
Slightly different from the traditional Gearman API, Net_geaman requires the user to implement function in the form of subclasses, such as example1.php file snippet:
classNet_Gearman_Job_Example1extendsNet_Gearman_Job_Common{// arg 是数组,与client里面的入参array对应publicfunctionrun($arg) {$cmd$arg[0]; //...return$result; }}
One of the techniques in example one is that the client calls the Example1, which is a non-existent method, using the _call mechanism. It doesn't work.
Example 2:
The client side introduces task and set concepts so that you can add multiple tasks to a collection, emit them together, and the client fragment:
$set=new Net_Gearman_Set();$task=new Net_Gearman_Task ('Example1'array ('date'));$set->addTask ($task);$client->runSet ($set);
Example 3:
Added the callback function after the completion of the task, the callback parameters have function name, handle (package good jobserver:number, mainly uniquely identify this task), and the return value of the task.
$task->attachCallback ("complete",Net_Gearman_Task::TASK_COMPLETE);//...function complete ($func, $handle, $result) { gm::log_msg ("[gm_client] complete ($handle/$func)"); gm::log_msg ("[gm_client] result: " . $result['result']);}
Example 4:
The main display is the transmission of data through complete back recall.
Example 5:
The client side adds a fail callback, and when the function throws a net_gearman_job_exception exception, it triggers a callback of type fail, code snippet:
classNet_Gearman_Job_Example3extendsNet_Gearman_Job_Common{publicfunctionrun($arg) {if (count ($arg1) { thrownew Net_Gearman_Job_Exception ("must provide exactly one command to run"); } //...
$task->attachCallback ("complete",Net_Gearman_Task::TASK_COMPLETE);$task->attachCallback ("fail",Net_Gearman_Task::TASK_FAIL);//...function fail ($task) { gm::log_msg ("[gm_client] fail, task: " . print_r ($task, true));}
However, the exception content is not passed back to the fail callback function, so it is recommended to use the normal complete callback, where the user can capture the failure information and put it in the return value.
Example 6:
The worker uses the monitor function, the code snippet:
$worker->beginWork ('monitor');// ...functionmonitor($idle, $time_of_last_job){$idle_str = ($idle'idle''not idle'; $time_of_last_job_str = date ('r'$time_of_last_job); gm::log_msg ("[gm_worker] status: $idle_str, time of last job: $time_of_last_job_str");}
The triggering events are:
Before the job starts.
After the job is completed.
Wait for the job, one minute to trigger.
Example 7:
The main thing is to add the task Start,complete and fail callback to the worker side, you can do something in it, code snippet:
$worker->attachCallback ('job_start', Net_Gearman_Worker::JOB_START); $worker->attachCallback ('job_complete', Net_Gearman_Worker::JOB_COMPLETE); $worker->attachCallback ('job_fail', Net_Gearman_Worker::JOB_FAIL);
Example 8:
is a large synthesis, the main points to be noted are as follows:
1. Although the author likes php very much, but also think that let PHP assume a daemon worker is not very insurance, may involve the abnormal exit Ah, memory use unreasonable ah what, may affect performance, so he provides a strategy: Each worker has an instance number, Use a file lock to ensure that an instance number only one worker runs, the worker exits automatically after processing a certain number of jobs, and uses the crontab task to pull the worker in seconds.
2. The worker's Beginworker exit can be judged in the monitor callback, which has this feature: The callback function returns false and the worker continues loop, returning true to loop exit, This is also confirmed in the source code of the worker:
if (call_user_func($monitor$idle$lastJobtrue) {$workingfalse;}
The above describes the gearman-php of the Net_gearman Library learning, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.