没有项目描述
muffin-redis的Python项目详细描述
muffin redis–redis对muffin框架的支持。
内容
Requirements
- python>;=3.3
Usage
在您的muffin应用程序配置中添加muffin_redis到插件。
或者像这样手动安装:
redis = muffin_redis.Plugin(**{'options': 'here'}) app = muffin.Application('test') app.install(redis)
Appllication configuration options
REDIS_DB–redis数据库数(0) REDIS_HOST–连接IP地址(“127.0.0.1”) REDIS_PORT–连接端口(6379) REDIS_PASSWORD–连接密码(无) REDIS_POOLSIZE–连接池大小(1) REDIS_FAKE–在测试建议中使用假的redis而不是真的redis(false)
Queries
@app.register def view(request): value = yield from app.ps.redis.get('my_key') return value
Pub/Sub
为此功能,您需要在配置中启用redis_pubsub=true。
发布消息非常简单:
@app.register def view(request): yield from app.ps.redis.publish('channel', 'message')
接收更复杂。 为了开始从pubsub接收消息,应该创建订阅管理器。 然后在管理器中打开一个单独的连接, 之后您可以订阅和收听消息:
sub = app.ps.redis.start_subscribe() yield from sub.open() # this creates separate connection to redis # sub.open() returns that manager itself, so this can be written like this: # sub = yield from app.ps.redis.start_subscribe().open() sub.subscribe(['channel1']) sub.psubscribe(['channel.a.*', 'channel.b.*']) # you can use masks as well # now wait for new messages while True: msg = yield from sub.next_published() print('got message', msg) print('the message itself:', msg.value) if shall_stop: break yield from sub.close() # don't forget to close connection!
Subscription Manager还使用接口实现PEP 0492异步,并为接口实现异步, 所以在python 3.5+中,可以用更轻的方式拼写:
async with app.ps.redis.start_subscribe() as sub: await sub.subscribe(['channel1']) await sub.psubscribe(['channel.a.*', 'channel.b.*']) # you can use masks as well async for msg in sub: print('got message', msg) # no need to close connection explicitly # as it will be done automatically by context manager.
为每个订阅管理器创建单独的redis连接可能不太好 (例如,每个websocket),因此可以通过管理订阅的通道掩码来改进 以及重用同一个“pubsub-specific”redis连接。
Bug tracker
如果您有任何建议、错误报告或 烦恼请向问题追踪者报告 在https://github.com/klen/muffin-redis/issues
Contributing
松饼redis的开发发生在:https://github.com/klen/muffin-redis
Contributors
- klen(基里尔·克莱诺夫)
License
根据MIT license授权。
如果你想对这个项目表示感谢,欢迎你寄来 明信片寄往:
Kirill Klenov pos. Severny 8-3 MO, Istra, 143500 Russia