This article mainly introduces how to install and start gearman and how to use pythonAPI. if you need it, refer to this article to describe how to install and start gearman and how to use python APIs, it is very useful for website construction and server maintenance!
I. Overview:
Gearman is an excellent task distribution framework for distributed computing. The installation and startup of the gearmand service and the installation of the python module of gearman are as follows:
Operating system: rnel 5.7
1. First, we needInstall gearmandIn centos and rhel environments, run the following command:
Yum install gearmand-y
Note: If you do not want to install gearmand through yum, you can compile and install through the source code, the specific installation method can refer to: http://www.jb51.net/article/51999.htm
After installation, you can start the gearmand service:
gearmand -p 4730 -L 10.22.10.90 --log-file=/tmp/gearmand-4730.log --pid-file=/tmp/gearmand-4730.pid -d
2.Install python-gearman
Python module of gearman, for: https://pypi.python.org/pypi/gearman/2.0.2
Run the following command to install the tool ):
easy_install gearman
pip install gearman
You can also install the SDK through the source code:
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
In this way, the python-gearman installation is completed.
II. example:
Below is a simple python example:
First, we need to write a worker. the code is as follows:
1. File name: 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. write the client as follows:
File name: 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
Note that the IP address in the above GearmanClient (['192. 168.12.34: 100']) must be based on the actual IP address and port number of the gearmand service.
3. then, run the following command:
python echoWorker.pypython echoClient.py
Now, the output is displayed.