并行化此代码
Parallelize this code
我想弄清楚如何并行化以下代码。
我已经查找了 joblib
、concurrent.futures
和 multiprocessing
模块,但我无法通过阅读文档和搜索来弄清楚它们是如何工作的 SO/google.
Grid
是一个定义了适当方法的 class 对象,循环的处理顺序无关紧要。
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
您可以像这样使用线程库:
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()
将在每次迭代的新线程中调用。 t.join()
然后等待线程完成。
我想弄清楚如何并行化以下代码。
我已经查找了 joblib
、concurrent.futures
和 multiprocessing
模块,但我无法通过阅读文档和搜索来弄清楚它们是如何工作的 SO/google.
Grid
是一个定义了适当方法的 class 对象,循环的处理顺序无关紧要。
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
您可以像这样使用线程库:
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()
将在每次迭代的新线程中调用。 t.join()
然后等待线程完成。