祝你有美好的一天,
今天我正在将代码从threading
移动到multiprocess
。一切似乎都很好,直到我收到以下错误:
错误
Traceback (most recent call last):
File "run.py", line 93, in <module>
main()
File "run.py", line 82, in main
emenu.executemenu(components, _path)
File "/home/s1810979/paellego/lib/execute/execute_menu.py", line 29, in executemenu
e.executeall(installed, _path)
File "/home/s1810979/paellego/lib/execute/execute.py", line 153, in executeall
pool.starmap(phase2, args)
File "/usr/lib64/python3.4/multiprocessing/pool.py", line 268, in starmap
return self._map_async(func, iterable, starmapstar, chunksize).get()
File "/usr/lib64/python3.4/multiprocessing/pool.py", line 608, in get
raise self._value
File "/usr/lib64/python3.4/multiprocessing/pool.py", line 385, in _handle_tasks
put(task)
File "/usr/lib64/python3.4/multiprocessing/connection.py", line 206, in send
self._send_bytes(ForkingPickler.dumps(obj))
File "/usr/lib64/python3.4/multiprocessing/reduction.py", line 50, in dumps
cls(buf, protocol).dump(obj)
_pickle.PicklingError: Can't pickle <class 'module'>: attribute lookup module on builtins failed
<小时 />代码
execute.py
def executeall(components, _path):
args = []
manager = multiprocessing.Manager()
q = manager.Queue()
resultloc = '/some/result.log'
for component in components:
for apkpath, resultpath in zip(execonfig.apkpaths, execonfig.resultpaths):
args.append((component,apkpath,resultpath,q,)) #Args for subprocesses
cores = askcores()
with multiprocessing.Pool(processes=cores) as pool:
watcher = pool.apply_async(lgr.log, (resultloc+'/results.txt', q,))
pool.starmap(phase2, args)
component.py
class Component(object):
def __init__(self, installmodule, runmodule, installerloc, installationloc, dependencyloc):
self.installmodule = installmodule
self.runmodule = runmodule
self.installerloc = installerloc
self.installationloc = installationloc
self.dependencyloc = dependencyloc
self.config = icnf.Installconfiguration(installerloc+'/conf.conf')
#lots of functions...
installconfig.py
class State(Enum):
BEGIN=0 #Look for units
UNIT=1 #Look for unit keypairs
KEYPAIR=3
class Phase(Enum):
NONE=0
DEPS=1
PKGS=2
class Installconfiguration(object):
def __init__(self, config):
dictionary = self.reader(config) #Fill a dictionary
#dictionary (key:Phase, value: (dictionary key: str, job))
self.deps = dictionary[Phase.DEPS]
self.pkgs = dictionary[Phase.PKGS]
job.py
class Job(object):
def __init__(self, directory=None, url=None):
self.directory = directory if directory else ''
self.url = url if url else ''
如您所见,我将一个组件作为参数传递给function phase2(component, str, str, multiprocess.manager.Queue())
。
component
构造函数的第二个和第三个参数是使用importlib
导入的模块。
我试过什么
我是Python的新手,但不是编程新手。这是我尝试过的:
- 因为错误本身并没有指出问题到底是什么,所以我尝试删除参数以找出哪些不能腌制:删除
component
,一切正常,所以这似乎是麻烦的原因。但是,我需要将此对象传递给我的进程。 - 我在互联网上搜索了几个小时,但除了关于多处理的基本教程和关于泡菜如何工作的解释之外,什么也找不到。我确实发现这句话应该有效,但不是在窗户或其他东西上。但是,它不适用于Unix(我使用)
我的想法
据我了解,没有任何迹象表明我不能发送包含两个 importlib 模块的类。我不知道component
类的确切问题是什么,但是importlib模块作为成员是唯一的非常规内容。这就是为什么我认为问题发生在这里。
问题
你知道为什么一个包含模块的类不适合"酸洗"吗?如何更好地了解错误发生的原因和位置Can't pickle <class 'module'>
?
更多代码
完整的源代码可以在 https://github.com/Sebastiaan-Alvarez-Rodriguez/paellego
向我提出的问题
如果您希望我编辑此问题,请留下评论要求澄清/更多代码片段/???
最后一个请求
我希望解决方案仅使用python标准库,最好使用python 3.3。另外,我的代码的一个要求是它在Unix系统上运行。
提前致谢
>编辑根据要求,这里有一个最小的示例,它大大简化了问题:
main.py(你可以像python main.py foo一样执行)
#!/usr/bin/env python
import sys
import importlib
import multiprocessing
class clazz(object):
def __init__(self, moduly):
self.moduly = moduly
def foopass(self, stringy):
self.moduly.foo(stringy)
def barpass(self, stringy, numbery):
self.moduly.bar(stringy)
print('Second argument: '+str(numbery))
def worker(clazzy, numbery):
clazzy.barpass('wow', numbery)
def main():
clazzy = clazz(importlib.import_module(sys.argv[1]))
clazzy.foopass('init')
args = [(clazzy, 2,)]
with multiprocessing.Pool(processes=2) as pool:
pool.starmap(worker, args)
if __name__ == "__main__":
main()
foo.py(上述调用建议需要位于同一目录中):
#!/usr/bin/env python
globaly = 0
def foo(stringy):
print('foo '+stringy)
global globaly
globaly = 5
def bar(stringy):
print('bar '+stringy)
print(str(globaly))
这在运行时出错:TypeError: can't pickle module objects
现在我们知道酸洗模块对象(可悲地)是不可能的。
为了摆脱错误,让我们不要clazz
模块作为属性,无论多么方便,而是让它取"modpath
",这是importlib
导入用户指定的模块所需的字符串。
它看起来像这样(foo.py 与上面完全相同):
#!/usr/bin/env python
import sys
import importlib
import multiprocessing
class clazz(object):
def __init__(self, modpathy):
self.modpathy = modpathy
def foopass(self, stringy):
moduly = importlib.import_module(self.modpathy)
moduly.foo(stringy)
def barpass(self, stringy, numbery):
moduly = importlib.import_module(self.modpathy)
moduly.bar(stringy)
print('Second argument: '+str(numbery))
def worker(clazzy, number):
clazzy.barpass('wow', number)
def main():
clazzy = clazz(sys.argv[1])
clazzy.foopass('init')
args = [(clazzy, 2,)]
with multiprocessing.Pool(processes=2) as pool:
pool.starmap(worker, args)
if __name__ == "__main__":
main()
如果你要求你的全局变量(如globaly
)保证保持状态,那么你需要传递一个可变对象(例如列表、字典)来保存这些数据,谢谢@DavisHerring:
模块属性在 Python 中被称为"全局变量",但它们并不比任何其他数据更持久或更易于访问。为什么不直接使用字典?
示例代码如下所示:
#!/usr/bin/env python
import sys
import importlib
import multiprocessing
class clazz(object):
def __init__(self, modpathy):
self.modpathy = modpathy
self.dictionary = {}
def foopass(self, stringy):
moduly = importlib.import_module(self.modpathy)
moduly.foo(stringy, self.dictionary)
def barpass(self, stringy, numbery):
moduly = importlib.import_module(self.modpathy)
moduly.bar(stringy, self.dictionary)
print('Second argument: '+str(numbery))
def worker(clazzy, number):
clazzy.barpass('wow', number)
def main():
clazzy = clazz(sys.argv[1])
clazzy.foopass('init')
args = [(clazzy, 2,)]
with multiprocessing.Pool(processes=2) as pool:
pool.starmap(worker, args)
if __name__ == "__main__":
main()
foo.py(不再有全局变量):
#!/usr/bin/env python
def foo(stringy, dictionary):
print('foo '+stringy)
globaly = 5
dictionary['globaly'] = globaly
def bar(stringy, dictionary):
print('bar '+stringy)
globaly = dictionary['globaly']
print(str(globaly))
这样,您可以在不烦人的can't pickle ...
错误的情况下解决问题,同时保持状态