【发布时间】:2021-11-10 09:59:15
【问题描述】:
我正在编写一个程序,旨在包装 C 代码以启动相位计采集。
这段代码应该同时在多个进程上运行,所以我使用multiprocessing。
我阅读了关于这个主题的几乎所有已回答的问题,但我认为我遗漏了一些东西。如果有人可以帮我解决这个问题,我会很高兴。
我有以下错误:
wdir='/home/castaing/Documents/LISA/lisa_zifo_monitoring/Python Drivers/Phasemeter/Py_Code') Traceback(最近一次调用最后一次):
文件“/home/castaing/Documents/LISA/lisa_zifo_monitoring/Python Drivers/Phasemeter/Py_Code/DriverPhasemeter.py”,第 297 行,在 Process.map(start,Phasemeter.phase_list)
文件 “/home/castaing/anaconda3/envs/LISA/lib/python3.7/multiprocessing/pool.py”, 第 268 行,在地图中 return self._map_async(func, iterable, mapstar, chunksize).get()
文件 “/home/castaing/anaconda3/envs/LISA/lib/python3.7/multiprocessing/pool.py”, 第 657 行,在获取 提高self._value
文件 “/home/castaing/anaconda3/envs/LISA/lib/python3.7/multiprocessing/pool.py”, 第 431 行,在 _handle_tasks 中 放(任务)
文件 “/home/castaing/anaconda3/envs/LISA/lib/python3.7/multiprocessing/connection.py”, 第 206 行,在发送中 self._send_bytes(_ForkingPickler.dumps(obj))
文件 “/home/castaing/anaconda3/envs/LISA/lib/python3.7/multiprocessing/reduction.py”, 第 51 行,在转储中 cls(buf, 协议).dump(obj)
AttributeError: Can't pickle local object 'CDLL.init.._FuncPtr'
我尝试定义一个全局函数来调用相位计对象的方法,如下所示。一开始我在相位计对象中有一个多启动方法,代码是:
def multistart(self) :
with multiprocessing.Pool(len(Phasemeter.phase_list)) as Process :
Process.map(lambda x :x.start,Phasemeter.phase_list)
这是代码(我只放了与我相关的部分):
#%% Initialization
#%% Function definition
#Fix pickle problem ?
def start(Phasemeter_object):
Phasemeter_object.start()
#%% Class definiton
class Phasemeter :
# List of all phasemeters objects, accessed by calling Phasemeter.phase_list
phase_list=[]
#%% Initialization
def __init__(self,*args,**kwargs) :
#%% Robustness. Check type of passed arguments
#%% Path setting, parsing config file
#%% Option handling
#%% Debug, used only if verbose argument is passed in start method
#%% Defining path to shared object file
self.path=os.path.abspath(os.path.join(os.getcwd(),
self.path_to_so_file))
# LIBC is now an object and its method are C Code's functions
self.LIBC = ctypes.CDLL(self.path)
# Settings C library's signature datatype with ctypes data structre tool
# INFO: To see all datas types that can be transmited to C Code
# read ctypes documentation
# First argument is int : argc
# Second argument is string array : argv
# Third is a string : path_to_log_file
self.LIBC.lisaf_phasemeter_main.argtypes= [ctypes.c_int,
ctypes.POINTER(ctypes.c_char_p),
ctypes.c_char_p,]
# Return type is int : Return code
self.LIBC.lisaf_phasemeter_main.restypes = [ctypes.c_int,]
# Add object to phase_list, list used in multistart method
Phasemeter.phase_list.append(self)
#%% Start
def start(self):
#%% Marshalling data for C library
# Create a string array with option list length size
self.c_char_pointer_array = ctypes.c_char_p * len(self.options)
# Encode option list
self.encoded_options = [str.encode(str(i)) for i in self.options ]
# Fill the string array with encoded strings
# REMINDER: C code only understand encoded strings
self.encoded_options = self.c_char_pointer_array (*self.encoded_options)
#%% Calling C library wihth encoded options
# If the logfile option is activated then the encoded
# string is transmited
if self.encoded_path_to_log_file :
self.status = self.LIBC.lisaf_phasemeter_main(
len(self.encoded_options),
self.encoded_options,
self.encoded_path_to_log_file)
# Otherwise None pointer is transmited
else :
self.status = self.LIBC.lisaf_phasemeter_main(len(self.encoded_options),
self.encoded_options,None)
#%% Multistart
if __name__ == '__main__' :
# This function is used to start acquisition on multiple phasemeters
my_phase = Phasemeter(name="PH1")
my_phase = Phasemeter(name="PH2")
with multiprocessing.Pool(len(Phasemeter.phase_list)) as Process :
Process.map(start,Phasemeter.phase_list)
【问题讨论】:
-
这就是我的想法,但我尝试了建议的解决方案,但它不起作用
-
但是我可能遗漏了什么,欢迎任何帮助
标签: python multiprocessing pickle