如何使用 recv_bytes_into 接收 ctypes 结构

问题描述

我受到 this Dave Beazley article 的启发,寻找一种超级简单的方法来使用套接字和多处理侦听器/客户端发送图像和相机命令。我明确地试图避免任何序列化依赖项,并尽可能减少这种依赖。下面是我尝试的一个最小示例。

class Netimage(ctypes.Structure):
    _pack_ = 1
    _fields_ = [
        ('width',ctypes.c_uint32),('height',('channels',ctypes.c_uint8),('bits_per_pixel',('pixels',ctypes.c_ubyte * imsZ)
    ]

    def __init__(self,width=WIDTH,height=HEIGHT,channels=3,pixels=(ctypes.c_ubyte * imsZ).from_buffer_copy(b'0'*imsZ)):
        self.width    = width
        self.height   = height
        self.channels = channels
        self.pixels   = pixels
    
    def __repr__(self):
        return f'width: {self.width},height: {self.height},channels: {self.channels},bits_per_pixel: {self.bits_per_pixel}'

# listen.py. listens for a connection and sends an image.
def main():
    im = Netimage()
    addr = ('localhost',4445)
    with Listener(addr,authkey=b'4445') as listener:
        with listener.accept() as conn:
            conn.send_bytes(im)


# client.py. Receives images from the listener
from multiprocessing.connection import Client
import socket

def main():
    im   = Netimage()
    addr = ('localhost',4445)

    with Client(addr,authkey=b'4445') as conn:
        conn.recv_bytes_into(im)
        print(im)

但这行不通。我收到以下错误

conn.recv_bytes_into(im) 文件 “/usr/lib/python3.8/multiprocessing/connection.py”,第 242 行,在 recv_bytes_into result.readinto(m[offset // itemsize :
类型错误:无效的 0-dim 内存索引

我认为这与像素阵列有关,所以我将其注释掉了,但没有任何改变。我也尝试过使用和不使用 _pack_=1谁能解释为什么它会抱怨 0-dim 内存以及我如何解决它?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)