我试图弄清楚如何并行化以下代码。
我查询了joblib
,concurrent.futures
和multiprocessing
模块,但在我的生活中不能通过阅读文档和搜索SO / google来了解它们的工作原理。
Grid
是一个定义了适当方法的类对象,并且处理循环的顺序无关紧要。
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
答案 0 :(得分:1)
您可以像这样使用线程库:
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()
然后等待线程完成。