网上很多资料对python gearman的使用都是用的gearman模块,然而这个模块仅支持python2.x ,使用python3的pip install安装,会因为不支持的python2语法报错,根本无法使用。 想用python3的资料特别少。
安装
首先先安装gearman:sudo apt-get install gearman libgearman-dev
安装python3对应的gearman模块
安装:pip install python3-gearman
如果系统里有多个版本的python,注意使用python3 -m pip install python3-gearman
避免安装到别的版本里去。
最简示例:
server端:
import python3_gearman
gm_worker = python3_gearman.GearmanWorker(['localhost:4730'])
# 定义收到消息的处理函数,返回值必须是个byte string。
def task_listener(gearman_worker, gearman_job):
print(gearman_job.data)
return ""
# 注册消息处理,msg为自定义的,和client保持一致
gm_worker.register_task('msg', task_listener)
# 启动监听
gm_worker.work()
client端:
import python3_gearman
gm_client = python3_gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])
gm_client.submit_job("msg", "hello world") # 发送消息,msg是自定义的,要和server端协商保持一致
先启动server,然后运行client。成功的话,client非阻塞,每次运行,server端会打印出对应的字符串。
进阶实例:
- 两端发送和接收解析相对复杂的json数据:
# server端:将上面的tesk_listener函数改为如下即可: j = json.loads(gearman_job.data) # client端:用json.dumps将一个复杂的字典传入,代替原来的"hello world即可"
- 客户端对发送结果进行校验
submit_job的返回值是一个结果定值,可以通过校验这个值来判断发送是否成功:import python3_gearman from python3_gearman.job import JOB_UNKNOWN def check_request_status(job_request): if job_request.complete: print ("Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)) elif job_request.timed_out: print ("Job %s timed out!" % job_request.unique) elif job_request.state == JOB_UNKNOWN: print ("Job %s connection failed!" % job_request.unique) gm_client = python3_gearman.GearmanClient(['localhost:4730', 'otherhost:4730']) submitted_job_request = gm_client.submit_job("msg", "hello world") check_request_status(submitted_job_request)
- 设置数据高优先级,设置发送方式为后台非阻塞模式
可以通过submit_job的参数进行配置,priority=python3_gearman.PRIORITY_HIGH
指定高优先级,background=True
指定通过后台非阻塞的方式发送。submitted_job_request = gm_client.submit_job("msg", "hello world", priority=python3_gearman.PRIORITY_HIGH, background=True)
完整代码:
server端:
import python3_gearman
import json
gm_worker = python3_gearman.GearmanWorker(['localhost:4730'])
# 定义收到消息的处理函数,返回值必须是个byte string。
def task_listener(gearman_worker, gearman_job):
print(gearman_job.data)
j = json.loads(gearman_job.data)
print(json.dumps(j,sort_keys=True,indent=4, ensure_ascii=False,separators=(',',':'))) # 格式化输出json字符串
return ""
# 注册消息处理,msg为自定义的,和client保持一致
gm_worker.register_task('msg', task_listener)
# 启动监听
gm_worker.work()
client端:
import python3_gearman
from python3_gearman.job import JOB_UNKNOWN
import json
def check_request_status(job_request):
if job_request.complete:
print ("Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result))
elif job_request.timed_out:
print ("Job %s timed out!" % job_request.unique)
elif job_request.state == JOB_UNKNOWN:
print ("Job %s connection failed!" % job_request.unique)
gm_client = python3_gearman.GearmanClient(['localhost:4730', 'otherhost:4730'])
msg_dict = {
"name":"threedog",
"age":18,
"class":{
"name":"一班",
"number":1
}
}
submitted_job_request = gm_client.submit_job("msg", json.dumps(msg_dict), priority=python3_gearman.PRIORITY_HIGH, background=False)
check_request_status(submitted_job_request)
文档
对于任何框架和工具,文档都是非常重要的一部分。比如上面的例子中,我们想知道除了JOB_UNKNOWN
还有什么其他的状态?除了PRIORITY_HIGH
还有什么可选的优先级。这些都需要借助文档。
然而python3-gearman这个小项目网上资料很少,而且没有找到官网文档的地址。
好在,潜心研究之下还是找到了官方提供的文档所在:这个项目是托管在github上的:https://github.com/josiahmwalton/python3-gearman 点开这个项目你会发现有自带的docs
只不过这个里面放的并不是html或者md格式的文档,而是一些rst后缀的文本文件和一个Makefile。这是一种很常用的文档生成的格式,rst就是文档源格式,便于书写,但是可读性较差。可以通过在这个目录下运行执行make +格式
的方式,生成指定格式的文档。
操作方法如下:
# 安装工具
sudo apt-get install python3-sphinx
# 文档下载
git clone https://github.com/josiahmwalton/python3-gearman.git
# 文档生成
cd python3-gearman/docs/
make html # 指定为html格式,直接输入make会提示有哪些可选的格式
完成后会在docs目录下生成一个_build目录,里面包含详细的官方文档和实例,我上面的示例代码也是从官方文档的实例中抽丝剥茧扒出来的: