- 使用multiprocessing创建多进程,每个进程下运行一个YOLOv5程序,可能会遇到如下问题:
pycuda._driver.LogicError: cuDeviceGet failed: initialization error
Traceback (most recent call last):
File “/usr/lib/python3.6/multiprocessing/process.py”, line 258, in _bootstrap
self.run()
File “/usr/lib/python3.6/multiprocessing/process.py”, line 93, in run
self._target(*self._args, **self._kwargs)
File “a.py”, line 15, in dott
ctx = cuda.Device(0).make_context()
pycuda._driver.LogicError: cuDeviceGet failed: initialization error
解决方案:出现这种问题的原因是主进程与子进程之前没有隔离开,上下文和启动方法出问题了,可以在创建进程前加入这一行
multiprocessing.set_start_method('spawn')
具体如下所示:
import multiprocessing as mp
def foo(q):
q.put('hello')
if __name__ == '__main__':
mp.set_start_method('spawn')
q = mp.Queue()
p = mp.Process(target=foo, args=(q,))
p.start()
print(q.get())
p.join()
- 按照第一种情况修改也可能出现以下问题:
Traceback (most recent call last):
File “”, line 2, in
File “/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/context.py”, line 242, in set_start_method
raise RuntimeError(‘context has already been set’)
RuntimeError: context has already been set
解决方案:在程序中 set_start_method() 不应该被多次调用。多个类似进程时会出现。
可以使用 get_context() 来获取上下文对象。上下文对象与多处理模块具有相同的API,并允许在同一程序中使用多个启动方法。
import multiprocessing as mp
def foo(q):
q.put('hello')
if __name__ == '__main__':
ctx = mp.get_context('spawn')
q = ctx.Queue()
p = ctx.Process(target=foo, args=(q,))
p.start()
print(q.get())
p.join()