Python中ctypes.structure的序列化

时间:2019-01-25 13:14:55

标签: python python-multiprocessing pathos

我正在尝试使用ctypes处理Python包装器。修改制造商给出的示例,我将帧作为ctypes.structure对象发送,效果很好。但是该对象无法序列化,因此无法为单独的进程设置队列。我尝试使用基于莳萝的病痛:

import ctypes
from pathos.helpers import mp
import time

#Define point structure
class HeliosPoint(ctypes.Structure):
    #_pack_=1
    # projector maximum seems to be uint12
    _fields_ = [('x', ctypes.c_uint16),
                ('y', ctypes.c_uint16),
                ('r', ctypes.c_uint8),
                ('g', ctypes.c_uint8),
                ('b', ctypes.c_uint8),
                ('i', ctypes.c_uint8)]
frameType = HeliosPoint * 1000

def fill_queue_with_frames(Qf):
    while 1:
        frame = frameType()
        Qf.put(frame)

frame_q = mp.Queue(1000)

ProcessB = mp.Process(target=fill_queue_with_frames, args=(frame_q,))
ProcessB.start()

使用悲哀我得到以下错误:

Traceback (most recent call last):
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/site-packages/multiprocess/queues.py", line 237, in _feed
    obj = _ForkingPickler.dumps(obj)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/site-packages/multiprocess/reduction.py", line 54, in dumps
    cls(buf, protocol).dump(obj)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 409, in dump
    self.save(obj)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 521, in save
    self.save_reduce(obj=obj, *rv)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 610, in save_reduce
    save(args)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 736, in save_tuple
    save(element)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 476, in save
    f(self, obj) # Call unbound method with explicit self
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/site-packages/dill/_dill.py", line 1293, in save_type
    StockPickler.save_global(pickler, obj)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 922, in save_global
    (obj, module_name, name))
_pickle.PicklingError: Can't pickle <class '__main__.HeliosPoint_Array_1000'>: it's not found as __main__.HeliosPoint_Array_1000
Traceback (most recent call last):
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 269, in _getattribute
    obj = getattr(obj, subpath)
AttributeError: module '__main__' has no attribute 'HeliosPoint_Array_1000'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 918, in save_global
    obj2, parent = _getattribute(module, name)
  File "/home/smaug/anaconda3/envs/Laser_p3.6/lib/python3.6/pickle.py", line 272, in _getattribute
    .format(name, obj))
AttributeError: Can't get attribute 'HeliosPoint_Array_1000' on <module '__main__' from 'test_queue.py'>

这可以解决吗?是否有另一种方法可以设置两个并发进程而无需序列化?

1 个答案:

答案 0 :(得分:1)

实际上,问题在于您需要一个没有定义的名称。将以下内容添加到模块中:

HeliosPoint_Array_1000 = HeliosPoint * 1000

这似乎可以解决去渍错误。