按照這篇英文貼文學習的
原文碼的備份
範例1:
像我們展示了Net_Geaman的基本架構,client端主要程式碼:
<code><span>// 一个client对象,需要jobserver信息,一个数组</span><span>$client</span> = <span>new</span> Net_Gearman_Client (gm::<span>$servers</span>); <span>// Example1就是function name</span><span>$client</span>->Example1 (<span>array</span> (<span>'date'</span>));</code>
worker端主要程式碼:
<code><span>// worker对象,需要jobserver信息</span><span>$worker</span> = <span>new</span> Net_Gearman_Worker (gm::<span>$servers</span>); <span>// 传入function name,给他处理Example1的能力</span><span>$worker</span>->addAbility (<span>'Example1'</span>); <span>// loop</span><span>$worker</span>->beginWork ();</code>
和傳統的gearman api略有差別,Net_Geaman需要使用者以子類別形式實作function,例如Example1.php檔案程式碼片段:
<code><span><span>class</span><span>Net_Gearman_Job_Example1</span><span>extends</span><span>Net_Gearman_Job_Common</span> {</span><span>// arg 是数组,与client里面的入参array对应</span><span>public</span><span><span>function</span><span>run</span><span>(<span>$arg</span>)</span> {</span><span>$cmd</span> = <span>$arg</span>[<span>0</span>]; <span>//...</span><span>return</span><span>$result</span>; } }</code>
範例一中的一個技巧是client呼叫了Example1這個不存在的方法,使用的是_call 機制。沒啥用。
範例2:
client側引入task和set概念,這樣就可以累積多個任務到一個集合,一起發出,client片段:
<code><span>$set</span><span>=</span><span>new</span> Net_Gearman_Set(); <span>$task</span><span>=</span><span>new</span> Net_Gearman_Task (<span>'Example1'</span>, <span>array</span> (<span>'date'</span>)); <span>$set</span><span>-></span>addTask (<span>$task</span>); <span>$client</span><span>-></span>runSet (<span>$set</span>);</code>
範例3:
增加了完成任務後的回呼功能,回呼的參數有function name,句柄(包好jobserver:number,主要是唯一標識一下這次任務),還有任務的回傳值。
<code>$task->attachCallback (<span>"complete"</span>,Net_Gearman_Task::TASK_COMPLETE); //<span>...</span><span>function</span> complete ($func, $handle, $result) { gm::log_msg (<span>"[gm_client] complete ($handle/$func)"</span>); gm::log_msg (<span>"[gm_client] result: "</span> . $result[<span>'result'</span>]); }</code>
範例4:
主要是展示透過complete回呼回傳資料。
例5:
展示client端加入fail回呼,當function中拋出Net_Gearman_Job_Exception異常時候,會觸發fail類型的回調,程式碼片段:
<code><span><span>class</span><span>Net_Gearman_Job_Example3</span><span>extends</span><span>Net_Gearman_Job_Common</span> {</span><span>public</span><span><span>function</span><span>run</span><span>(<span>$arg</span>)</span> {</span><span>if</span> (count (<span>$arg</span>) != <span>1</span>) { <span>throw</span><span>new</span> Net_Gearman_Job_Exception (<span>"must provide exactly one command to run"</span>); } <span>//...</span></code>
<code>$task->attachCallback (<span>"complete"</span>,Net_Gearman_Task::TASK_COMPLETE); $task->attachCallback (<span>"fail"</span>,Net_Gearman_Task::TASK_FAIL); //<span>...</span><span>function</span> fail ($task) { gm::log_msg (<span>"[gm_client] fail, task: "</span> . print_r ($task, true)); }</code>
但異常內容不會回傳給fail回呼函數,所以還是建議使用正常的complete回調,使用者可以自己捕獲失敗訊息放到返回值裡面。
例6:
worker裡面使用了monitor功能,程式碼片段:
<code><span>$worker</span>->beginWork (<span>'monitor'</span>); <span>// ...</span><span><span>function</span><span>monitor</span><span>(<span>$idle</span>, <span>$time_of_last_job</span>)</span> {</span><span>$idle_str</span> = (<span>$idle</span>) ? <span>'idle'</span> : <span>'not idle'</span>; <span>$time_of_last_job_str</span> = date (<span>'r'</span>, <span>$time_of_last_job</span>); gm::log_msg (<span>"[gm_worker] status: $idle_str, time of last job: $time_of_last_job_str"</span>); }</code>
其觸發事件有:
job開始前。
job完成後。
等待job時候,一分鐘觸發一次。
範例7:
主要是在worker端加入了任務的start,complete和fail回調,可以在裡面做點事情,程式碼片段:
<code><span>$worker</span><span>-></span>attachCallback (<span>'job_start'</span>, Net_Gearman_Worker<span>::JOB_START</span>); <span>$worker</span><span>-></span>attachCallback (<span>'job_complete'</span>, Net_Gearman_Worker<span>::JOB_COMPLETE</span>); <span>$worker</span><span>-></span>attachCallback (<span>'job_fail'</span>, Net_Gearman_Worker<span>::JOB_FAIL</span>);</code>
範例8:
是一大綜合,主要有以下幾點值得注意:
1. 作者雖然很喜歡php,但也認為讓php承擔一個daemon的worker不是很保險的事情,可能涉及異常退出啊、內存使用不合理呀什麼的,有可能影響性能,所以他提供了一個策略:每個worker有個實例編號,使用檔案鎖定來確保一個實例編號只有一個worker運行,worker在處理了一定數目的job後自動退出,使用crontab任務秒級拉起worker。
2. worker的beginWorker退出可以在monitor回呼中判斷,monitor回調有這種特性:回呼函數回傳false則worker繼續loop,回傳true則loop退出,這個在worker的源碼中也得到證實:
<code><span>if</span> (call_user_func(<span>$monitor</span>, <span>$idle</span>, <span>$lastJob</span>) == <span>true</span>) { <span>$working</span> = <span>false</span>; }</code>
以上就介紹了gearman-php的Net_Gearman庫學習,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。