DataLoader windows平台下 多线程读数据报错 | BrokenPipeError: [Errno 32] Broken pipe | freeze

it2022-05-05  120

DataLoader 多线程读取数据

使用DataLoader读取数据时,为了加快效率,所以使用了多个线程,即num_workers不为0,在windows下报了错误。

DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)

https://github.com/pytorch/pytorch/pull/5585 这里是官方说明了一些原因,应该是windows下的一些线程文件读写的问题

在Windows上,FileMapping对象应必须在所有相关进程都关闭后,才能释放。 启用多线程处理时,子进程将创建FileMapping,然后主进程将打开它。 之后当子进程将尝试释放它的时候,因为父进程还在引用,所以它的引用计数不为零,无法释放。 但是当前代码没有提供在可能的情况下再次关闭它的机会。这个版本官方说num_workers=1是可以用的,更多的线程还在解决,不过现在即便是用2个子进程也已经可以了。

解决方法有二(各自均亲测有效)

num_workers=0,也就是单线程操作在使用DataLoader读取之前加上  if name == '__main__' :  就可以了。

报错信息如下:

in _Popen is not going to be frozen to produce an executable.''') return _default_context.get_context().Process._Popen(process_obj) RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase. File "multiprocessing\context.py", line 322, in _Popen This probably means that you are not using fork to start your child processes and you have forgotten to use the proper idiom return Popen(process_obj) in the main module: if __name__ == '__main__': File "multiprocessing\popen_spawn_win32.py", line 89, in __init__ freeze_support() ... The "freeze_support()" line can be omitted if the program is not going to be frozen to produce an executable. reduction.dump(process_obj, to_child)

 


最新回复(0)