Rumah > pembangunan bahagian belakang > Tutorial Python > 分布式任务系统GEARMAN FOR PYTHON初探

分布式任务系统GEARMAN FOR PYTHON初探

高洛峰
Lepaskan: 2017-04-18 10:33:19
asal
1184 orang telah melayarinya

了解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
Salin selepas log masuk

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

安装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
Salin selepas log masuk

获取直接用自动安装: 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()
Salin selepas log masuk

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

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
Salin selepas log masuk
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan