首頁 > 後端開發 > Python教學 > 分散式任務系統GEARMAN FOR PYTHON初探

分散式任務系統GEARMAN FOR PYTHON初探

高洛峰
發布: 2017-04-18 10:33:19
原創
1175 人瀏覽過

了解Gearman,請造訪gearman官網:gearman.org/

+++++++++++++++++++++++++++++++++++++ +++++++++

安裝Gearman:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++

基礎依賴函式庫:

yum install boost-devel libevent-devel sqlite-devel libuuid-devel
wget https://launchpad.net/gearmand/trunk/0.33/+download/gearmand-0.33.tar.gz 
tar xzvf gearmand-0.33.tar.gz 
cd gearmand-0.33 
./configure
make
make install
登入後複製

+++++++++++++++++++++++++++++++ +++++++++++++

安裝Gearman Python客戶端

++++++++++++++++++++++++++++ ++++++++++++++++

wget http://pypi.python.org/packages/source/g/gearman/gearman-2.0.2.tar.gz#md5=3847f15b763dc680bc672a610b77c7a7 
    tar xvzf  gearman-2.0.2.tar.gz 
    python setup.py install
登入後複製

獲取直接用自動安裝: easy_install gearman

啟動服務:gearmand -d 

啟動 Worker:gearman -w -f wc -- wc -l &

-w 代表啟動的是worker,-f wc 代表啟動一個task名字為wc, -- wc -l表示這個task是做wc -l 統計行數。

啟動Client:gearman -f wc < /etc/passwd

++++++++++++++++++++++++++++++++++ ++++++++++

python work程式碼:

++++++++++++++++++++++++++++++++ ++++++++++++

import os 
import gearman 
import math       
class MyGearmanWorker(gearman.GearmanWorker):   
    def on_job_execute(self, current_job):   
        print "Job started" 
        return super(MyGearmanWorker, self).on_job_execute(current_job)   
     
def task_callback(gearman_worker, gearman_job):   
    print gearman_job.data  
    return gearman_job.data 
     
my_worker = MyGearmanWorker([&#39;192.168.0.75:4730&#39;])   
my_worker.register_task("echo", task_callback)   
my_worker.work()
登入後複製

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++++

python client程式碼:

+++++++++++++++++++++++++++++++++ +++++++++++

from gearman import GearmanClient       
gearman_client = GearmanClient([&#39;192.168.0.75:4730&#39;]) 
gearman_request = gearman_client.submit_job(&#39;echo&#39;, &#39;foo&#39;) 
result_data = gearman_request.result 
print result_data
登入後複製
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板