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
- // Create our client object
- $client = new GearmanClient();
- // Add a server
- $client->addServer(); // by default host/port will be "localhost" & 4730
- echo "Sending job ";
- // Send reverse job
- $result = $client->doNormal("reverse", "Hello!");
- if ($result) {
- echo "Success: $result ";
- }
4.worker.py(本文使用Python作为worker):
- 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()的异步机制: