【Gearman学习笔记】分布式处理入门

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在CODE上查看代码片派生到我的代码片
 
  1. <?php  
  2.   
  3. // Create our client object  
  4. $client = new GearmanClient();  
  5.   
  6. // Add a server  
  7. $client->addServer(); // by default host/port will be "localhost" & 4730  
  8.   
  9. echo "Sending job ";  
  10.   
  11. // Send reverse job  
  12. $result = $client->doNormal("reverse", "Hello!");  
  13. if ($result) {  
  14.   echo "Success: $result ";  
  15. }  



4.worker.py(本文使用Python作为worker):

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import gearman  
  2.   
  3. gm_worker = gearman.GearmanWorker(['localhost:4730'])  
  4.   
  5. def task_listener_reverse(gearman_worker, gearman_job):  
  6.     print 'Reversing string: ' + gearman_job.data  
  7.     return gearman_job.data[::-1]  
  8.   
  9. # gm_worker.set_client_id is optional  
  10. gm_worker.set_client_id('python-worker')  
  11. gm_worker.register_task('reverse', task_listener_reverse)  
  12.   
  13. # Enter our work loop and call gm_worker.after_poll() after each time we timeout/see socket activity  
  14. 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/

原文地址:https://www.cnblogs.com/caicaizi/p/4878320.html