我有以下的簡單的類定義:爲什麼Python酸洗庫抱怨不存在的類成員?
def apmSimUp(i):
return APMSim(i)
def simDown(sim):
sim.close()
class APMSimFixture(TestCase):
def setUp(self):
self.pool = multiprocessing.Pool()
self.sims = self.pool.map(
apmSimUp,
range(numCores)
)
def tearDown(self):
self.pool.map(
simDown,
self.sims
)
凡類APMSim由普通簡單的python原始類型(字符串,列表等),唯一的例外是一個靜態成員,這是一個多經理純粹定義。名單
然而,當我試圖執行這個班,我得到了以下錯誤信息:
Error
Traceback (most recent call last):
File "/home/peng/git/datapassport/spookystuff/mav/pyspookystuff_test/mav/__init__.py", line 77, in setUp
range(numCores)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
MaybeEncodingError: Error sending result: '[<pyspookystuff.mav.sim.APMSim object at 0x7f643c4ca8d0>]'. Reason: 'TypeError("can't pickle thread.lock objects",)'
爲thread.lock不能在任何地方發現了奇怪的是,我嚴格避免任何multithreadi ng組件(如您所見,只使用多處理組件)。並且這些組件中沒有一個存在於我的類中,或者只存在於靜態成員中,我應該怎麼做才能使這個類可用?
順便說一句,有沒有一種方法可以排除酸洗的黑羊成員?像Java的@transient註解一樣?
非常感謝您的幫助!
UPDATE:以下是我的全APMSim類,請看看你是否發現任何違反其picklability:
usedINums = mav.manager.list()
class APMSim(object):
global usedINums
@staticmethod
def nextINum():
port = mav.nextUnused(usedINums, range(0, 254))
return port
def __init__(self, iNum):
# type: (int) -> None
self.iNum = iNum
self.args = sitl_args + ['-I' + str(iNum)]
@staticmethod
def create():
index = APMSim.nextINum()
try:
result = APMSim(index)
return result
except Exception as ee:
usedINums.remove(index)
raise
@lazy
def _sitl(self):
sitl = SITL()
sitl.download('copter', '3.3')
sitl.launch(self.args, await_ready=True, restart=True)
print("launching .... ", sitl.p.pid)
return sitl
@lazy
def sitl(self):
self.setParamAndRelaunch('SYSID_THISMAV', self.iNum + 1)
return self._sitl
def _getConnStr(self):
return tcp_master(self.iNum)
@lazy
def connStr(self):
self.sitl
return self._getConnStr()
def setParamAndRelaunch(self, key, value):
wd = self._sitl.wd
print("relaunching .... ", self._sitl.p.pid)
v = connect(self._getConnStr(), wait_ready=True) # if use connStr will trigger cyclic invocation
v.parameters.set(key, value, wait_ready=True)
v.close()
self._sitl.stop()
self._sitl.launch(self.args, await_ready=True, restart=True, wd=wd, use_saved_data=True)
v = connect(self._getConnStr(), wait_ready=True)
# This fn actually rate limits itself to every 2s.
# Just retry with persistence to get our first param stream.
v._master.param_fetch_all()
v.wait_ready()
actualValue = v._params_map[key]
assert actualValue == value
v.close()
def close(self):
self._sitl.stop()
usedINums.remove(self.iNum)
懶裝飾是從這個庫:
https://docs.python.org/2/tutorial/classes.html#generator-expressions
但它是一個靜態成員,是否應該將靜態成員視爲與頂級變量相同的方式? – tribbloid
已將唯一的多處理管理器列表移出到全局成員中,但問題依然存在。請參閱我的更新。 – tribbloid
@tribbloid:不,它不是很簡單,它們。如果你是Google,那麼已經有很多關於SO的討論。爲什麼你不能只展示你想要醃製的課程? –