Skip to content

Instantly share code, notes, and snippets.

@acgtyrant
Last active August 8, 2018 03:42
Show Gist options
  • Save acgtyrant/b595529571316f19fe3cfbef600f488a to your computer and use it in GitHub Desktop.
Save acgtyrant/b595529571316f19fe3cfbef600f488a to your computer and use it in GitHub Desktop.
torch.multiprocessing 被阻塞
import torch
def _process(queue):
input_ = queue.get()
queue.put((input_,))
def test_syncbn():
torch.multiprocessing.set_start_method('spawn')
sub_input_1 = torch.randn(2, 4, 4, 4).cuda(0)
queue_1 = torch.multiprocessing.Queue()
queue_1.put(sub_input_1)
process_1 = torch.multiprocessing.Process(target=_process, args=(queue_1,))
process_1.start()
process_1.join()
result_1 = queue_1.get()
print('end')
print(result_1)
if __name__ == '__main__':
test_syncbn()
@acgtyrant
Copy link
Author

首先创建虚拟环境,再 pip install torch,torch 包蛮大的。加载虚拟环境后,直接执行 test.py,然后它就阻塞了,end 没有打印出来。ctrl-c 中断会发现它阻塞在 result_1 = queue_1.get() 一行,为什么会阻塞呢?还是说有什么我没发现的超低级 bug?

PS:创建虚拟环境的 Python 至少要为 3.5 以上,大概。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment