如何实现redis的pubsub超时功能?
我想使用Redis的pubsub功能来实现comet,但是pubsub没有超时,所以如果我使用ps.listen(),它会阻塞,即使客户端关闭浏览器。
Greenlet 在生成过程中具有超时功能,但我不知道如何将它们结合起来。
Flask的伪代码:
@app.route('/')
def comet():
rc = redis.Redis()
ps = rc.pubsub()
ps.subscribe('foo')
for item in ps.listen():
if item['type'] == 'message':
return item['data']
# ps.listen() will block, so how to make it timeout after 30 s?
I want to use Redis's pubsub feature to implement comet, but pubsub doesn't have timeout, so if I use ps.listen()
, it will block, even if client closes browser.
Greenlet has a timeout feature when spawn process, but I don't know how to combine them.
Flask's pseudo code:
@app.route('/')
def comet():
rc = redis.Redis()
ps = rc.pubsub()
ps.subscribe('foo')
for item in ps.listen():
if item['type'] == 'message':
return item['data']
# ps.listen() will block, so how to make it timeout after 30 s?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为你不是线程(我假设这是故意的,并且在某些情况下是明智的),所以你必须使用某种类型的中断。信号是 Unix 系统上的一种中断,允许您在可能阻塞的调用期间返回回调。
这个打开的文件永远不会返回的示例符合您想要做的事情。它取自 http://docs.python.org/library/signal.html# module-signal
但是一个警告。由于 Python 使用全局解释器锁来执行操作系统信号处理,因此会遇到一些稳定性问题。不过,这些问题通常应该很少见。
Because you're not threading (and I'm assuming this is intentional and in some cases wise) you must use a type of interrupt. Signals are a type of interrupt on Unix systems to allow you to return to a callback during a call that could block.
This example of a file open which will never return is in line with what you want to do. It's taken from http://docs.python.org/library/signal.html#module-signal
But a warning. Because Python uses a Global Interpreter Lock to perform OS signal handling it is subject to some stability problems. These problems should be rare normally though.