阅读27 返回首页    go 汽车大全


淘宝开源metaq的python客户端

    前面一篇博客介绍了我在github上的一个metaq分支,今天下午写了个metaq的python客户端,目前仅支持发送消息功能,不过麻雀虽小,五脏俱全,客户端和zookeeper的交互和连接管理之类都还具备,不出意外,我们会首先用上。第一次正儿八经地写python代码,写的不好的地方请尽管拍砖,多谢。
    项目叫meta-python,仍然放在github上:https://github.com/killme2008/meta-python

    使用需要先安装zkpython这个库,具体安装这篇博客,使用很简单,发送消息:
    from metamorphosis import Message,MessageProducer,SendResult
    p=MessageProducer("topic")
    message=Message("topic","message body")
    print p.send(message)
    p.close()

    
MessageProducer就是消息发送者,它的构造函数接受至少一个topic,默认的zk_servers为localhost:2181,可以通过zk_servers参数指定你的zookeeper集群:

p=MessageProducer("topic",zk_servers="192.168.1.100:2191,192.168.1.101:2181")

更多参数请直接看源码吧。一个本机的性能测试(meta和客户端都跑在我的机器上,机器是Mac MC700,osx 10.7,磁盘没有升级过):
from metamorphosis import Message,MessageProducer
from time import time
p=MessageProducer("avos-fetch-tasks")
message=Message("avos-fetch-tasks","https://www.taobao.com")
start=time()
for i in range(0,10000):
    sent=p.send(message)
    if not sent.success:
        print "send failed"
finish=time()
secs=finish-start
print "duration:%s seconds" % (secs)
print "tps:%s msgs/second" % (10000/secs)
p.close()

 结果:


duration:1.85962295532 seconds
tps:5377.43415749 msgs/second
文章转自庄周梦蝶  ,原文发布时间2012-03-21

最后更新:2017-05-18 20:36:19

  上一篇:go  《Netty 实战》Netty In Action中文版 第2章——你的第一款Netty应用程序(二)
  下一篇:go  《Netty 实战》Netty In Action中文版 第2章——你的第一款Netty应用程序(一)