gpt4 book ai didi

python - 如何使用posix消息队列在 Node 和python之间进行IPC?

转载 作者:行者123 更新时间:2023-12-02 14:48:49 33 4
gpt4 key购买 nike

我正在尝试在 Node js 和 python3 应用程序之间编写 IPC 的“hello world”。目前,当新消息到达 Python 应用程序时,我遇到了解析错误。代码是:

python :

from ipcqueue import posixmq
from time import sleep

q1 = posixmq.Queue('/fila1')

while True:

while q1.qsize() > 0:
print('p1: Recebi na minha fila: ' + str(q1.get()))

sleep(0.5)

Node :

const PosixMQ = require('posix-mq')

var mq = new PosixMQ();
mq.open({
name: '/fila1',
create: true,
mode: '0777',
maxmsgs: 10,
msgsize: 11
});
mq.push("hello world")
mq.close();

当第二个应用程序发送消息时,python 应用程序失败并显示:

File "../test-fila1.py", line 9, in

print('p1: Recebi na minha fila: ' + str(q1.get()))   File "/usr/lib/python3.7/site-packages/ipcqueue/posixmq.py", line 174, in

get

return self._serializer.loads(data)   File "/usr/lib/python3.7/site-packages/ipcqueue/serializers.py", line 14,

in loads

return pickle.loads(data) KeyError: 101

[2]- Exit 1 python3 ../test-fila1.py[3]+ Done node index.js

编辑
因此,我决定将问题从“KeyError: 101 when loading ipc message between node and python apps”更改为“How to IPC between node and python using posix message queue?”因为我刚刚注意到我发布的示例代码每次运行时都会产生不同的错误。现在发生的错误是(但是,没有更改代码):

Traceback (most recent call last): File "snippets/test-fila1.py",
line 9, in
print('p1: Recebi na minha fila: ' + str(q1.get())) File "/usr/lib/python3.7/site-packages/ipcqueue/posixmq.py", line 174, in
get
return self._serializer.loads(data) File "/usr/lib/python3.7/site-packages/ipcqueue/serializers.py", line 14,
in loads
return pickle.loads(data)
_pickle.UnpicklingError: unpickling stack underflow

最佳答案

如果您查看 Queue__init__

class Queue(object):
"""
POSIX message queue.
"""

def __init__(self, name, maxsize=10, maxmsgsize=1024, serializer=PickleSerializer):

如您所见,默认的 serializerPickleSerializer,这意味着当您从 发送原始数据时,它假定队列中的数据已被 pickle node.js.修复很简单,使用 RawSerializer

from ipcqueue import posixmq
from time import sleep
from ipcqueue.serializers import RawSerializer
q1 = posixmq.Queue('/fila1', serializer=RawSerializer)

while True:

while q1.qsize() > 0:
print('p1: Recebi na minha fila: ' + str(q1.get()))

sleep(0.5)

Working fine

关于python - 如何使用posix消息队列在 Node 和python之间进行IPC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57135741/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com