Python 多处理 -> 无法在线程内生成进程
Python multiprocessing -> can not spawn Process within thread
我通过创建一个继承自 threading.Thread 的对象在 __main__ 函数内创建一个线程。在其 运行 方法中,我使用模块的全局命名空间中的目标函数打开 multiprocessing.Process,但出现错误:
from multiprocessing import Process, Queue
import threading
def executeTests(ScriptName, Params, MsgQueue, ResultQueue):
...
class TestRunner(threading.Thread):
def __init__(self, tests):
threading.Thread.__init__(self)
...
def run(self):
MsgQueue = Queue()
ResultQueue = Queue()
TestProcess = Process(target=executeTests, args=(ScriptName, Params, MsgQueue, ResultQueue))
TestProcess.start()
...
if __name__ == "__main__":
TestRunner(...).start()
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 105, in sp
awn_main
exitcode = _main(fd)
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 115, in _m
ain
self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'executeTests' on <module '__main__' (built-
in)>
我自己解决了。
main 中缺少 join()。
我通过创建一个继承自 threading.Thread 的对象在 __main__ 函数内创建一个线程。在其 运行 方法中,我使用模块的全局命名空间中的目标函数打开 multiprocessing.Process,但出现错误:
from multiprocessing import Process, Queue
import threading
def executeTests(ScriptName, Params, MsgQueue, ResultQueue):
...
class TestRunner(threading.Thread):
def __init__(self, tests):
threading.Thread.__init__(self)
...
def run(self):
MsgQueue = Queue()
ResultQueue = Queue()
TestProcess = Process(target=executeTests, args=(ScriptName, Params, MsgQueue, ResultQueue))
TestProcess.start()
...
if __name__ == "__main__":
TestRunner(...).start()
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 105, in sp
awn_main
exitcode = _main(fd)
File "C:\Program Files\Python36\lib\multiprocessing\spawn.py", line 115, in _m
ain
self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'executeTests' on <module '__main__' (built-
in)>
我自己解决了。 main 中缺少 join()。