How to reuse buffers passed through a Queue

Hello, I hope you are doing well!

Following the multiprocessing Best Practices page, a best practice is stated to

…make it send the buffers back

There have been other forum posts about clarifying this point (see this one or this one) but those did not get any conclusive answers to the question.

User phizaz suggested this near the bottom of the second post:

def run(Qr, Qs):
    while True:
        b = Qr.get()
        torch.randn(..., out=b) # reusing the buffer tensor without re-allocating

def main():
    for i in range(buffer_size): # allocate buffers
        b = torch.empty(...)
    ... start processes ...
    while True:
        b = Qr.get() # main's Qr = run's Qs
        ... use b ...
        Qs.put(b) # main's Qs = run's Qr

Though he mentioned

Caveat: I see the cost of an additional queue to be huge. It might not be worth the benefit of not reallocating additional shared memory.

Which I think he is probably correct about. So what exactly was the best practices page alluding to?

Very interested in a response. Thank you!