Competition for pipe resources while trying to receive PyTorch tensors

I met a problem that sub-processes in python will compete for pipe resources when one subprocess tries to receive PyTorch tensors and another subprocess requests a different pipe connection.

Here is my code (run on Ubuntu 18.04):

import time
from multiprocessing import Process, Pipe
import torch


def send(conn):
    a, b = Pipe()
    data = [torch.rand(1) for _ in range(20)]
    print('start sending', time.time())
    conn.send(data)
    print('finish sending', time.time())

    while True:
        if a.poll():
            pass
        # else:
        #    time.sleep(0.1)


def recv(conn):
    while True:
        if conn.poll():
            print('start receiving', time.time())
            conn.recv()
            print('finish receiving', time.time())
            break
        else:
            time.sleep(0.1)


if __name__ == '__main__':
    recv_conn, send_conn = Pipe()

    send_proc = Process(target=send, args=(send_conn,))
    recv_proc = Process(target=recv, args=(recv_conn,))

    send_proc.start()
    recv_proc.start()

    recv_proc.join()
    send_proc.terminate()

This code needs several seconds to receive the torch tensors. But if I uncomment the last two lines in the send method, the speed of receiving the torch tensors will be very quick.

And this problem only appears while sending torch tensors.

Does anyone know the reason for this problem?

cc @colesbury @VitalyFedyunin regarding multiprocessing with torch.tensors

Hi Rohan, thanks for your reply. I’ve solved this problem, and I just want to know the reason for it. Can you give me some advice?