Home > Backend Development > Python Tutorial > A preliminary study on distributed task system GEARMAN FOR PYTHON

A preliminary study on distributed task system GEARMAN FOR PYTHON

高洛峰
Release: 2017-04-18 10:33:19
Original
1175 people have browsed it

To learn about Gearman, please visit gearman official website: gearman.org/

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

Install Gearman:

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

Basic dependency library:

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
Copy after login

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

Install Gearman Python client

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

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
Copy after login

Get the automatic installation directly: easy_install gearman

Start the service: gearmand -d

Start the Worker: gearman -w -f wc -- wc -l &

-w means to start a worker, -f wc means to start a task named wc, -- wc -l means that this task is to do wc -l to count the number of rows.

Start Client: gearman -f wc < /etc/passwd

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

python work code:

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

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()
Copy after login

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

python client code:

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

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
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template