Search code examples
pythonconcurrencymultiple-processes

Concurrent futures in python doesn't wait for processes to finish


I've got a function, main(), that generates 4 processes, and starts executes them eventually, but it loops over this whole block of code once it begins several times. What I want to have it do I think is apparent; print "hello1", execute the processes (and therefore the print statements), and then print "hello2"

What am I doing wrong? Would also be open to a solution that uses multiprocessing.Process() if that's easier, but I've had the same issue there as well.


def funcy(c):
    print("Hi " + str(c))
    for e in range(1000):
        e = e
    return c*2

print("hello1")

def main():
    with concurrent.futures.ProcessPoolExecutor() as executor:
        f = [1,2,3,4]
        results = executor.map(funcy,f)

        for result in results:
            print(result)

if __name__ == '__main__':
    main()

print("hello2")

Solution

  • I think the main problem is that the for loop is indented within the with context manager for the concurrent.futures call. Also, the print("hello1") seems out of place. The print("hello2") needs to be indented.

    I think the below edit is what you're looking for...maybe...

    import concurrent.futures
    
    def funcy(c):
        print("Hi " + str(c))
        for e in range(1000):
            e = e
        return c*2
    
    def main():
        with concurrent.futures.ProcessPoolExecutor() as executor:
            f = [1,2,3,4]
            results = executor.map(funcy,f)
    
        for result in results:
            print(result)
    
    if __name__ == '__main__':
        print("hello1")
    
        main()
    
        print("hello2")