This article describes the installation of Gearman and Python API usage examples, for website construction and server maintenance is very useful!
First, overview:
Gearman is a very good task distribution framework that can be used in distributed computing. The installation of the specific Gearmand service and the installation of the Gearman Python module as well as a simple example are as follows:
Operating system: Rnel 5.7
1. First, we need to install Gearmand, in the CentOS and RHEL environment, we just run the following command:
Yum Install Gearmand-y
Note: If you do not want to install Gearmand by yum, you can compile the installation through the source code, the specific installation method can refer to: http://www.jb51.net/article/51999.htm
After the installation is complete, 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. Installing Python-gearman
Gearman python module, download address: https://pypi.python.org/pypi/gearman/2.0.2
We can install it with the following command (two commands are available, two options):
Easy_install Gearman
Pip Install Gearman
Or you can install it 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, we have completed the installation of the Python-gearman.
Second, use example:
Below, a simple example of Python is listed:
First, we need to write a worker with the following code:
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"
2. Write the client as follows:
File name: echoclient.py
Note that the IP address in the above gearmanclient ([' 192.168.12.34:4730 ']) needs to be based on the IP address and port number of the actual boot Gearmand service.
3. Then, we run the following command:
Python Echoworker.pypython echoclient.py
At this point, you can see the output.