Tornado和Redis的新手,并实现了侦听器/工作器设置的开始。
我希望能够将LPUSH任务放入队列并将其RPOP关闭。 BRPOP似乎是弹出它们的最佳方式,因为如果当前没有,它将等待添加一个。问题是,每当我使用它时,它永远不会返回...但是当我使用RPOP时,我会按预期获得队列中的下一个项目。
class ListenHandler(tornado.websocket.WebSocketHandler):
uid = ''
CHANNEL_TPL = "client_%s"
RESPONSE_TPL = '{"command":"%s","rid":"%s","status":"%s","result":%s}'
def open(self):
# new websocket connection is established from a client
print "open socket"
self.uid = session = uuid4()
def on_message(self, message):
print "on_message called [%s]" % message
try:
m = json.loads(message)
except ValueError:
self.write_message('BAD')
return
# check for RID (request id)
if not 'rid' in m:
self.write_message('error: unspecified rid')
return
# confirm receipt of data
confirm_string = '%s OK' % (m['rid'])
self.write_message(confirm_string)
# check for command
if not 'command' in m:
response = '%s error: unspecified command' % (m['rid'])
self.write_message(response)
return
# process commands
if m['command'] == 'register':
self._register(m['rid'])
elif m['command'] == 'get_canvas':
self._queue_command('read', m)
elif m['command'] == 'save_canvas':
if 'data' in m:
self._queue_command('write', m)
else:
response = '%s unspecified data' % (m['rid'])
self.write_message(response)
return
elif m['command'] == 'list_command_queue':
self._list_command_queue(m['rid'])
elif m['command'] == 'get_read_job':
self._get_read_job(m['rid'])
elif m['command'] == 'get_write_job':
self._get_write_job(m['rid'])
else:
# no commands recognized
response = '%s error: unknown command' % (m['rid'])
self.write_message(response)
return
print "end of on_message()\n"
def callback(self, data):
print "- callback()"
self.write_message(data)
def on_close(self):
# websocket connection is closed by client
pass
def _register(self, rid):
data = '{"uid":"%s"}' % (self.uid)
response = self.RESPONSE_TPL % ('register', rid, 'completed', data)
self.callback(response)
@tornado.web.asynchronous
@tornado.gen.engine
def _queue_command(self, type, m):
channel = self.CHANNEL_TPL % (type)
print "pushing job to %s ... data[%s]" % (channel, m)
yield tornado.gen.Task(self.application.rdb.lpush, channel, m)
return
def _list_command_queue(self, rid):
channel_r = self.CHANNEL_TPL % ('read')
channel_w = self.CHANNEL_TPL % ('write')
data = '{"client_read":"%s","client_write":"%s"}' % (self.application.rdb.llen(channel_r), self.application.rdb.llen(channel_w))
response = self.RESPONSE_TPL % ('list_command_queue', rid, 'completed', data)
print "list_command_queue [%s]" % (response)
self.callback(response)
@tornado.web.asynchronous
@tornado.gen.engine
def _get_read_job(self, rid):
channel = self.CHANNEL_TPL % ('read')
data = yield tornado.gen.Task(self.application.rdb.rpop, (channel))
response = self.RESPONSE_TPL % ('get_read_job', rid, 'completed', data)
self.callback(response)
@tornado.web.asynchronous
@tornado.gen.engine
def _get_write_job(self, rid):
channel = self.CHANNEL_TPL % ('write')
data = yield tornado.gen.Task(self.application.rdb.rpop, (channel))
response = self.RESPONSE_TPL % ('get_write_job', rid, 'completed', data)
self.callback(response)
上面的类将把识别的命令和LPUSH分成两个不同的队列之一(写入SQL DB的作业的'write'队列,以及只读作业的'read'队列)。从理论上讲,使用BRPOP在不同的机器上会有许多“工作”线程来获取这些命令并执行它们。但是现在,我正在使用相同的监听器来测试队列中的什么。
函数get_write_jobs和get_read_jobs将返回队列中的下一个条目,没有问题。但是,如果我向任一方法添加'b'(brpop),该函数永远不会调用回调。似乎只是锁定,等待下一个可用条目,但那里有条目。
知道这里发生了什么吗?我误解了BRPOP的目的吗?
谢谢, 尼克
答案 0 :(得分:3)
也许有点晚了,但我想我可以提供帮助。
我今天遇到了同样的问题,它几乎让我发疯。最后,经过大量的调试后,我通过将密钥或通道传递到列表中的blpop / brpop(例如[channel])来解决它。
龙卷风'redp'brpop / blpop在内部做的是将密钥转换为列表,但是当只收到一个密钥时,它会将字符串转换为字符列表(简直太棒了......),这就是为什么之后调用块,它正在等待各种列表中的新项目,这些项目的名称对应于原始密钥的所有字符。