27
魔獸
淘寶開源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()
p=MessageProducer("topic")
message=Message("topic","message body")
print p.send(message)
p.close()
MessageProducer就是消息發送者,它的構造函數接受至少一個topic,默認的zk_servers為localhost:2181,可以通過zk_servers參數指定你的zookeeper集群:
更多參數請直接看源碼吧。一個本機的性能測試(meta和客戶端都跑在我的機器上,機器是Mac MC700,osx 10.7,磁盤沒有升級過):
結果:
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()
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
tps:5377.43415749 msgs/second
文章轉自莊周夢蝶 ,原文發布時間2012-03-21
最後更新:2017-05-18 20:36:19