Search code examples
pythonmultithreadingparallel-processingmultiprocessingconcurrent.futures

Parallelize this code


I am trying to figure out how to parallelize the following code. I have looked up joblib, concurrent.futures, and multiprocessing modules, but cannot for the life of me figure out how they work from reading the docs and scouring SO/google.

Grid is a class object with the appropriate methods defined and it does not matter what order the loop is processed.

def ProcessGrid(Grid):
    #Parallel This Loop
    for i in range(len(Grid)):
        Grid[i].AdjustCostMultiplier()
        Grid[i].FindAllNeighbours(Grid)
        print("Processing Grid Cell: " + str(i) + " of " + str(len(Grid)),end = "\r")
    #Return to serial
    return Grid

Solution

  • You can use the threading library like this:

    import threading
    
    def process(grid, i):
        grid[i].AdjustCostMultiplier()
        grid[i].FindAllNeighbours(Grid)
        print("Processing Grid Cell: " + str(i) + " of " + str(len(grid)), end = "\r")
    
    def ProcessGrid(Grid):
        threads = []
        for i in range(len(Grid)):
            t = threading.Thread(target=process, args=(Grid, i))
            t.start()
            threads.append(t)
        for t in threads:
            # Wait for all threads to finish
            t.join()
        #Return to serial
        return Grid
    

    process() will be called in a new thread for every iteration. t.join() then waits for the threads to finish.