PyTorch multiprocessing error: ConnectionRefusedError: [Errno 111] Connection refused

Viewed 365

When I run the following code,

import torch.multiprocessing as mp
import torch

def f(q):
    t = torch.ones([2,3])
    q.put(t)

if __name__ == '__main__':
    mp.set_start_method('spawn')
    q = mp.Queue()
    p1 = mp.Process(target=f, args=(q,))
    p2 = mp.Process(target=f, args=(q,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    a = q.get()
    b = q.get()
    print(a, b)

I get the error:

ConnectionRefusedError: [Errno 111] Connection refused

It seems that this error is caused by torch.ones([2,3]). If in method f I put something else into q, such as a list or a value, it works fine. I've tried several approaches but none of them works for me. I really have no idea why this happens and how to deal with it. Any help would be much appreciated.

0 Answers
Related