当前位置: 首页 > 工具软件 > py3k-gearman > 使用案例 >

gearman python 实例

彭令秋
2023-12-01

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
import json
 
def worker_ttlsa.com(gearman_worker,job):
    var_dump(job.data)
    param = json.loads(job.data)
    sleep = param['sleep']
    return str(sleep)
 
def main():
    gw = GearmanWorker(['127.0.0.1:4730'])
    gw.register_task("worker_ttlsa.com", worker_ttlsa.com)
    gw.work()
 
if __name__ == "__main__":
    main()

client:

from gearman import GearmanClient
from var_dump import var_dump
import json
 
def main():
    gc = GearmanClient(['127.0.0.1:4730'])
    param = {'msg':'www.ttlsa.com', 'sleep':5}
     
    request = gc.submit_job('worker_ttlsa.com',json.dumps(param))
    var_dump(request.result)
 
if __name__ == "__main__":
    main()

注意:worker一定要有retrun,且返回的内容必须是字符串。否则报下面的错误。

TypeError: Expecting byte string, got <type ‘NoneType’>

查看状态:

# telnet 127.0.0.1 4730
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
workers
36 127.0.0.1 - :
35 127.0.0.1 - : worker_ttlsa.com
.
status
worker_ttlsa.com    0       0       1
.

status输出分4个部分:已注册的任务,正在运行的任务,队列中的任务,可用的worker。



 类似资料: