用于延迟的阻塞和非阻塞(异步)客户端
lingerclient的Python项目详细描述
延迟客户端
--用于延迟的阻塞和非阻塞(异步)客户机
这个包为python客户机提供了与Linger服务器(一个消息队列和pubsub服务,带有rest http api)交互的功能。
使用Tornadohttp客户端在python 3中实现了延迟客户端。
安装
使用以下pip命令安装lingerclient
包:
pip install lingerclient
这也将确保依赖项包tornado
已安装。
示例代码
阻止客户端示例:
import lingerclient
lc = lingerclient.BlockingLingerClient()
r = lc.post('test-channel', {'msg': 'My first message'})
print(r)
print(lc.channels())
m = lc.get('test-channel')
lc.delete(m['id'])
stream
方法可用于迭代来自通道的流:
for m in lc.stream('test-channel'):
print(m['body'])
lc.delete(m['id'])
异步客户端示例:
导入延迟客户端 来自Tornado Import Ioloop,gen
@gen.coroutine
def run_test():
lc = lingerclient.AsyncLingerClient()
r = yield lc.post('test-channel', {'msg': 'My first message'})
print(r)
r = yield lc.channels()
print(r)
m = yield lc.get('test-channel')
yield lc.delete(m['id'])
ioloop.IOLoop.current().run_sync(run_test)
主要客户方法
channels()
列出活动频道
post(channel, body, **kwargs)
在频道上留言。接受查询参数的关键字参数:优先级、超时、传递和延迟。
get(channel, nowait=False)
从频道上得到消息返回带有消息ID、正文、通道等的A dict。如果没有消息可用,则不返回任何消息。将参数nowait
设置为True以防止长轮询
stream(channel, max_retries=0)
获取通道的流(迭代器)。参数max_retries可以限制失败的重新连接尝试次数默认值是max_retries=0,这意味着没有限制。
drain(channel)
排出通道中的消息(即,删除通道中的所有消息)。
channel_stats(channel)
获取频道统计信息
subscriptions(channel)
列出订阅频道的主题
subscribe(channel, topic, **kwargs)
订阅主题频道。接受查询参数的关键字参数:优先级、超时、传递和延迟
unsubscribe(channel, topic)
从主题中取消订阅频道
topics()
列出所有主题
publish(topic, body)
发布主题消息
subscribers(topic)
列出订阅主题的频道
delete(msg_id)
删除消息
stats()
获取服务器统计信息
其他客户端方法
__init__(linger_url=None, encode=json_encode, decode=json_decode, content_type='application/json', io_loop=None, **request_args)
方法签名对于AsyncLingerClient
和BlockingLingerClient
是相同的,因为blockinglingerclient是asynclingerclient的包装器,其中每个要延迟的http请求都在ioloop中运行。
所有参数都是可选的
参数linger_url
是延迟服务器的基url默认值是http://127.0.0.1:8989/
参数encode
和decode
用于提供自定义消息编码和解码功能。默认为json编码/解码。
参数content_type
应设置为编码函数输出的适当mime类型默认值是application/json
io_loop
被传递给asynchttpclient,用于连接。
当发出延迟请求时,request_args
中的关键字参数将被应用默认情况下,请求参数use_gzip
为true。访问本地延迟服务器时,可能需要将use_gzip
设置为false。
请求参数可以包括用于基本身份验证的auth_username
和auth_password
有关其他可能的参数,请参见tornado.httpclient.HTTPRequest
。
close()
关闭延迟客户端,释放所有使用的资源
closed
(属性)
指示是否关闭延迟客户端的布尔值
支持
该软件的支持可以在商业基础上提供,请参见www.nephics.com获取联系信息
许可证
代码和文档是在apache许可证v2.0下授权的,请参阅许可证文件中的更多内容。