编译安装Gearman、python简单测试
2013-06-10 23:47 游乐场123 阅读(348) 评论(0) 编辑 收藏 举报Gearman是开源、轻量级的任务分发程序框架,方便开发分布式的任务处理(计算)程序,Gearman支持多种后端数据的存储。
Gearman有c和java两个版本,支持多种语言的客户端
一、安装依赖库
sudo yum install boost-devel libevent-devel sqlite-devel libuuid-devel
编译Gearman是必须要有mysql开发库的
二、编译安装
./configure --with-mysql=/opt/db/Percona-Server-5.5.21-rel25.0/bin/mysql_config --prefix=/opt/gearmand-0.33
会列出已经支持的数据存储库
* Building with libsqlite3 yes * Building with libdrizzle no * Building with libmemcached no * Building with libpq no * Building with tokyocabinet no * Building with libmysql yes
编译安装
make && make install
三、编译中出现一些小问题
最新版的gearmand-0.34无法安装成功,会提示:
libgearman-server/plugins/queue/mysql/queue.cc:49:25: fatal error: mysql/mysql.h: No such file or directory
但是已经认出MySQL:
checking if MySQL version is >= 5.0... yes checking for mysqld... /opt/db/Percona-Server-5.5.21-rel25.0/bin/mysqld
限于时间紧迫,暂时使用gearmand-0.33
四、启动gearmand
./gearmand -L 127.0.0.1 -p 7003
五、编写测试代码(python)
先安装python-gearman
easy_install gearman
1、编写任务工作者:worker.py
#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- # file: worker.py import os import gearman import math class CustomGearmanWorker(gearman.GearmanWorker): def on_job_execute(self, current_job): print "开始......" print "工作中......" print '结束' return super(CustomGearmanWorker, self).on_job_execute(current_job) def task_callback(gearman_worker, job): print job.data return job.data new_worker = CustomGearmanWorker(['127.0.0.1:7003']) new_worker.register_task("echo", task_callback) new_worker.work()
2、启动worker.py
python worker.py
可以启动多个,分布在不同的机器上
3、编写测试端client.py
#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- # file: client.py from gearman import GearmanClient new_client = GearmanClient(['127.0.0.1:7003']) current_request = new_client.submit_job('echo', 'foo') new_result = current_request.result print new_result
4、测试一下
python client.py
可以看到worker输出信息了
使用体会
1、编写异步的任务分发程序简单方便
2、测试中发现,任务的分发不是很均匀(调度算法不好?)