标签:
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作为客户端):
4.worker.py(本文使用Python作为worker):
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://www.cnblogs.com/caicaizi/p/4878320.html