test.py 并且 main.py 位于同一文件夹中,并且我执行 main.py,解释器报告以下错误。
蟒蛇环境:蟒蛇2.7.15
如何解决这个问题?
谷歌告诉我这个问题在python3.4中得到了修复,但是我想在python 2.7中使用这种风格,该怎么做 test.py
from multiprocessing import Process, Queue
def interaction(q):
print '*'*50
q.put('-'*50)
q = Queue()
p = Process(target=interaction, args=(q,))
p.start()
p.join()
print q.get()
main.py
if __name__ == '__main__':
execfile('test.py')
错误:
Traceback (most recent call last):
File "C:sync360codepythonoobe_testmultiprocessing_testdemorundmain.py", line 3, in <module>
execfile('test.py')
File "test.py", line 13, in <module>
main()
File "test.py", line 10, in main
p.start()
File "C:Python27libmultiprocessingprocess.py", line 130, in start
self._popen = Popen(self)
File "C:Python27libmultiprocessingforking.py", line 277, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:Python27libmultiprocessingforking.py", line 199, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:Python27libpickle.py", line 224, in dump
self.save(obj)
File "C:Python27libpickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:Python27libpickle.py", line 425, in save_reduce
save(state)
File "C:Python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:Python27libpickle.py", line 655, in save_dict
self._batch_setitems(obj.iteritems())
File "C:Python27libpickle.py", line 687, in _batch_setitems
save(v)
File "C:Python27libpickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:Python27libpickle.py", line 754, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <function func at 0x02B3C1B0>: it's not found as __main__.func
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:Python27libmultiprocessingforking.py", line 381, in main
self = load(from_parent)
File "C:Python27libpickle.py", line 1384, in load
return Unpickler(file).load()
File "C:Python27libpickle.py", line 864, in load
dispatch[key](self)
File "C:Python27libpickle.py", line 886, in load_eof
raise EOFError
EOFError
[Finished in 0.3s with exit code 1]
我的预感是execfile()
的使用导致了这种情况,因为它使 Python 对__main__
是什么感到困惑。
您可以改为导入test
模块(顺便说一句,应该将其重命名为更具描述性的名称(。