分布式任務系統Gearman for Python Demo

來源:互聯網
上載者:User

瞭解Gearman,請訪問gearman官網:http://gearman.org/index.php?id=getting_started
Gearman for Python API Doc: http://pythonhosted.org/gearman/

++++++++++++++++++++++++++++++++++++++++++++
安裝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(['192.168.0.75:4730'])    
    my_worker.register_task("echo", task_callback)    
    my_worker.work() 

++++++++++++++++++++++++++++++++++++++++++++
python client代碼:
++++++++++++++++++++++++++++++++++++++++++++
    from gearman import GearmanClient       
    gearman_client = GearmanClient(['192.168.0.75:4730'])  
    gearman_request = gearman_client.submit_job('echo', 'foo')  
    result_data = gearman_request.result  
    print result_data

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.