Edit: For those finding this later, this is explicitly not a duplicate of the other topic that somebody linked to this. The problem is unrelated to stdout buffering, but rather was a misunderstanding of how imap_unordered was calling do_something
I'm trying to debug a separate issue which requires some print statements in my Python multiprocessing code. I can print() fine in the main processes, but when I spawn new processes, I can't successfully print anything.
Here is a bare bones example of my issue:
import argparse
from multiprocessing import Pool, get_context
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--foo', required=True)
    global args # need this if I'm reading from threads / forks
    args = parser.parse_args()
    print('This prints fine')
    with get_context('spawn').Pool(processes=4) as pool:
        pool.imap_unordered(do_something, [0, 1, 2, 3, 4 ])
    return
def do_something():
    print(args.foo * 2)
    print("this doesn't print either")
if __name__=="__main__":
    main()
What is the correct way to print from the do_something method?
Please note that I am using python from Bash on an Ubuntu 18.04 machine. I'm not using IDLE, or any IDE, which I see similar questions for.
Edit: Please note that the issue is also not that the printing is delayed, but rather that it does not happen at all. Even if I flush the stdout buffer in the do_something function.
Final edit: It looks like my code was not actually calling the do_something function. When I force it to through the methods described in below, I do see the expected prints. This code produces that output:
import collections
from multiprocessing import Pool, get_context
def main():
    print('This prints fine')
    with get_context('spawn').Pool(processes=4) as pool:
        results = collections.deque(pool.imap_unordered(do_something, [0, 1, 2, 3, 4 ]), 0)
    print(results)
    return
def do_something(i):
    print("this doesn't print either") # Actually does print
    return i
if __name__=="__main__":
    main()
 
     
     
    