Python热队列未与PHP Redis对话:pickle无效的加载键

2024-06-26 00:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在PHP中将项目添加到Redis队列/列表中,并在Python中获取它们。我安装了PHPRedis library并将一些项目放入队列,如下所示:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$conn_request = json_encode(['app_id' => $requestAr['app_id'], 'partner_id' => $user_id,
                                                     'request_epoch' => $current_time]);

$responseAr['queue'] = $redis->lpush("connections", $conn_request);

我用它来添加一些项目。如果然后通过redis-cli运行lrange connections 0 5,我会看到:

1) "{\"app_id\":\"1\",\"partner_id\":1,\"request_epoch\":1566090457}"
2) "{\"app_id\":\"1\",\"partner_id\":1,\"request_epoch\":1566089663}"
3) "{\"app_id\":\"1\",\"partner_id\":1,\"request_epoch\":1566089295}"

这看起来不错。因此,我安装了Python 3模块HotQueue,并尝试了以下代码:

from hotqueue import HotQueue

queue = HotQueue("connections", host="10.136.250.70")
queue.get()

它不返回任何内容。如果我运行queue.put('test-item'),然后运行queue.get(),我会看到我用Python添加的项目,但没有PHP中的项目。然后我使用命令keys *查看Redis cli,现在我看到:

1) "msg:foo"
2) "connections"
3) "fu"
4) "hotqueue:connections"

这似乎意味着在PHP中,我应该向hotqueue:connections添加项。我将PHP代码更改为:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$conn_request = json_encode(['app_id' => $requestAr['app_id'], 'partner_id' => $user_id,
                                                     'request_epoch' => $current_time]);

$responseAr['queue'] = $redis->lpush("hotqueue:connections", $conn_request);

现在在Redis CLI命令lrange hotqueue:connections 0 5中,我看到:

1) "{\"app_id\":\"1\",\"partner_id\":1,\"request_epoch\":1566093788}"
2) "\x80\x03X\x0b\x00\x00\x00test-item-1q\x00."

但是,如果我像以前一样运行相同的Python代码,我现在会得到一个错误:

line 104, in get
    msg = self.serializer.loads(msg)
_pickle.UnpicklingError: invalid load key, '{'.

我看到了类似错误的问题,但它们似乎与字符编码有关。我的PHP编码应该是UTF-8。这可能是JSON或字节编码的问题


Tags: 项目代码redisidapppartnerqueuerequest