Gearman installation startup and Python API usages _python

Source: Internet
Author: User

This article describes the installation of Gearman and Python API usage examples, for Web site building and server maintenance is very useful!

First, overview:

Gearman is a very good task distribution framework that can be used for distributed computing. The installation of specific Gearmand services and the installation of Gearman Python modules as well as a simple example are as follows:

Operating system: Rnel 5.7

1. First, we need to install Gearmand, in CentOS and rhel environments, we simply run the following command:

Yum Install Gearmand-y

Note: If you do not want to install the Gearmand through the Yum Way, you can compile the installation by 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. Install Python-gearman

Gearman python module, download address: https://pypi.python.org/pypi/gearman/2.0.2

We can use the following command to install (two commands are available, two choose one):

Easy_install Gearman
Pip Install Gearman

Or it can be installed through the source code:

wget https://pypi.python.org/packages/source/g/gearman/gearman-2.0.2.tar.gz--no-check-certificate
tar 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 Python-gearman.

Second, the use of examples:

Here's a simple example of Python:
First, we need to write a worker with the following code:

1. FileName: 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 client, as follows:
FileName: 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 the IP address in gearmanclient ([' 192.168.12.34:4730 ']) above, depending on the IP address and port number of the actual startup Gearmand service.

3. Then we run the following command:

Python echoworker.py
python echoclient.py

At this point, you can see the output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.