FROM: http://www.ttlsa.com/distributed-processing-systems/gearman-python-example/
有关gearman的介绍,可以参见本博客之前的文章。
gearman的Python客户端使用蛮简单的。具体方法参见:http://pythonhosted.org//gearman/library.html
worker:
from gearman import GearmanWorker |
from var_dump import var_dump |
def worker_ttlsa.com(gearman_worker,job): |
param = json.loads(job.data) |
gw = GearmanWorker([ '127.0.0.1:4730' ]) |
gw.register_task( "worker_ttlsa.com" , worker_ttlsa.com) |
if __name__ == "__main__" : |
client:
from gearman import GearmanClient |
from var_dump import var_dump |
gc = GearmanClient([ '127.0.0.1:4730' ]) |
param = { 'msg' : 'www.ttlsa.com' , 'sleep' : 5 } |
request = gc.submit_job( 'worker_ttlsa.com' ,json.dumps(param)) |
if __name__ = = "__main__" : |
注意:worker一定要有retrun,且返回的内容必须是字符串。否则报下面的错误。
TypeError: Expecting byte string, got <type ‘NoneType’>
查看状态:
Escape character is '^]' . |
35 127.0 . 0.1 - : worker_ttlsa.com |
status输出分4个部分:已注册的任务,正在运行的任务,队列中的任务,可用的worker。