gearman的安裝啟動及python API使用執行個體

來源:互聯網
上載者:User
本文講述了gearman的安裝啟動及python API使用執行個體,對於網站建設及伺服器維護來說非常有用!

一、概述:

Gearman是一款非常優秀的任務分發架構,可以用於分散式運算。具體的gearmand服務的安裝啟動及gearman的python 模組的安裝以及簡單樣本如下:

作業系統:rnel 5.7

1. 首先,我們需要安裝gearmand,在centos和rhel環境下,我們只需運行以下命令:

yum install gearmand -y

注意:如果不希望通過yum的方式來安裝gearmand,可以通過源碼編譯安裝,具體安裝方法可以參考:http://www.jb51.net/article/51999.htm

安裝完畢之後,就可以啟動gearmand服務:

gearmand -p 4730 -L 10.22.10.90 --log-file=/tmp/gearmand-4730.log --pid-file=/tmp/gearmand-4730.pid -d

2. 安裝python-gearman

gearman的python模組,下載地址為:https://pypi.python.org/pypi/gearman/2.0.2

我們可以用以下命令安裝(兩個命令均可,二選一):

easy_install gearman

pip install gearman

或者也可以通過源碼安裝:

wget https://pypi.python.org/packages/source/g/gearman/gearman-2.0.2.tar.gz --no-check-certificatetar zxvf gearman-2.0.2.tar.gz cd gearman-2.0.2 python setup.py install

這樣,我們就完成了python-gearman的安裝。

二、使用樣本:

下面,列舉一個簡單的python例子:
首先,我們需要編寫一個worker,代碼如下:

1.檔案名稱:echoWorker.py

#!/usr/bin/env python import os import gearman import math class MyGearmanWorker(gearman.GearmanWorker):   def on_job_execute(self, current_job):     print "Job started"     print "===================\n"     return super(MyGearmanWorker, self).on_job_execute(current_job) def task_callback(gearman_worker, gearman_job):   print gearman_job.data   print "-----------\n"   return gearman_job.data my_worker = MyGearmanWorker(['10.22.10.47:4730']) my_worker.register_task("echo", task_callback) my_worker.work() 

2.編寫client,如下:
檔案名稱:echoClient.py

#!/usr/bin/env python2.7 from gearman import GearmanClient gearman_client = GearmanClient(['192.168.12.34:4730']) gearman_request = gearman_client.submit_job('echo', 'test gearman') result_data = gearman_request.result print result_data 

注意上面GearmanClient(['192.168.12.34:4730'])中的IP地址,需要根據實際啟動gearmand服務的IP地址和連接埠號碼為準。

3.然後,我們運行以下命令:

python echoWorker.pypython echoClient.py

至此,即可看到輸出。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.