【Gearman学习笔记】分布式处理入门
2015-10-14 18:34
323 查看
1.首先,确保你已经安装了gearmand环境并且语言绑定(language binding)已经生效。
2.涉及到的各个部分:
ServerThe server, gearmand, will coordinate clients and workers ensuring that calls from the clients are delivered to workers and that results from workers are sent back to the client.gearmand作为我们的服务端,使client和worker协同工作,确保从client端发过来的请求能够传送到worker,同时使经过worker处理过的请求结果能够返回给client。ClientA process which has a blob of data to process, a known function which can process it, and a desire to get the processed results back. In our case, the client wants a string reversed.Client就是一个有很多待处理数据的进程,它能够被其他程序处理并得到处理后的返回结果。在我们的例子中,client端的需求是使一个字符串翻转。WorkerA process which connected to the server and offers to process function calls. In this example, the client can reverse strings.Worker是一个连接到server的进程,server端接收到client的请求后,把任务分派给worker,worker对请求作出处理。在我们的例子中,worker要做的任务是翻转一份字符串。
3.client.php(本文使用PHP作为客户端):
[php] view plaincopy
<?php
// Create our client object
$client = new GearmanClient();
// Add a server
$client->addServer(); // by default host/port will be "localhost" & 4730
echo "Sending job\n";
// Send reverse job
$result = $client->doNormal("reverse", "Hello!");
if ($result) {
echo "Success: $result\n";
}
4.worker.py(本文使用Python作为worker):
[python] view plaincopy
import gearman
gm_worker = gearman.GearmanWorker(['localhost:4730'])
def task_listener_reverse(gearman_worker, gearman_job):
print 'Reversing string: ' + gearman_job.data
return gearman_job.data[::-1]
# gm_worker.set_client_id is optional
gm_worker.set_client_id('python-worker')
gm_worker.register_task('reverse', task_listener_reverse)
# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
gm_worker.work()
5.测试:
测试环境配置:
Linux3.5.7-gentoo #1 x86_64 Intel(R) Xeon(R) CPU E5645 @ 2.40GHz GenuineIntel GNU/Linux
因为我在gentoo server下面,没法开两个terminal,所以,要先把worker.py作为后台进程运行:
然后,用php CLI运行client.php:
可以看到,在gearman的协调下,worker完成一次client的请求。
6.英文出处:http://gearman.org/examples/reverse/
7.【更新】
上面的例子讲的是同步机制,这还体现不出gearman的强大,下面是doBackground()的异步机制:
2.涉及到的各个部分:
ServerThe server, gearmand, will coordinate clients and workers ensuring that calls from the clients are delivered to workers and that results from workers are sent back to the client.gearmand作为我们的服务端,使client和worker协同工作,确保从client端发过来的请求能够传送到worker,同时使经过worker处理过的请求结果能够返回给client。ClientA process which has a blob of data to process, a known function which can process it, and a desire to get the processed results back. In our case, the client wants a string reversed.Client就是一个有很多待处理数据的进程,它能够被其他程序处理并得到处理后的返回结果。在我们的例子中,client端的需求是使一个字符串翻转。WorkerA process which connected to the server and offers to process function calls. In this example, the client can reverse strings.Worker是一个连接到server的进程,server端接收到client的请求后,把任务分派给worker,worker对请求作出处理。在我们的例子中,worker要做的任务是翻转一份字符串。
3.client.php(本文使用PHP作为客户端):
[php] view plaincopy
<?php
// Create our client object
$client = new GearmanClient();
// Add a server
$client->addServer(); // by default host/port will be "localhost" & 4730
echo "Sending job\n";
// Send reverse job
$result = $client->doNormal("reverse", "Hello!");
if ($result) {
echo "Success: $result\n";
}
4.worker.py(本文使用Python作为worker):
[python] view plaincopy
import gearman
gm_worker = gearman.GearmanWorker(['localhost:4730'])
def task_listener_reverse(gearman_worker, gearman_job):
print 'Reversing string: ' + gearman_job.data
return gearman_job.data[::-1]
# gm_worker.set_client_id is optional
gm_worker.set_client_id('python-worker')
gm_worker.register_task('reverse', task_listener_reverse)
# Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity
gm_worker.work()
5.测试:
测试环境配置:
Linux3.5.7-gentoo #1 x86_64 Intel(R) Xeon(R) CPU E5645 @ 2.40GHz GenuineIntel GNU/Linux
因为我在gentoo server下面,没法开两个terminal,所以,要先把worker.py作为后台进程运行:
然后,用php CLI运行client.php:
可以看到,在gearman的协调下,worker完成一次client的请求。
6.英文出处:http://gearman.org/examples/reverse/
7.【更新】
上面的例子讲的是同步机制,这还体现不出gearman的强大,下面是doBackground()的异步机制:
http://gearman.org/examples/send-emails/
相关文章推荐
- Wayland与Weston简介
- HDU 2824 The Euler function
- 八大排序算法
- ZOJ 3911 Prime Query
- HDU 1087 Super Jumping! Jumping! Jumping!
- linux中fork()函数详解(原创!!实例讲解) .
- ffmpeg 提取 视频,音频,字幕 方法
- WEB前端性能分析--工具篇
- Linux任务计划、周期性任务执行
- iOS开发网络数据之AFNetworking使用
- bash的位置变量和特殊变量
- 自定义控件(视图)56期笔记02:View的绘制流程
- myeclipse 于 否update software 解
- 黑马程序员——java中面向对象的三大特征
- java 用log4j输出日志信息
- iOS私人通讯录
- JS控制div显示隐藏
- 前端学习笔记
- VC 打开文件
- bash中的算术运算