python - 并行处理没有提高效率?
问题描述
我正在学习 AI 并阅读有关并行处理的信息。这是我复制解决骑士之旅问题的代码来测试的代码:
# Python3 program to solve Knight Tour problem using Backtracking
# Chessboard Size
# starting time
start = time.time()
n = 8
def isSafe(x, y, board):
'''
A utility function to check if i,j are valid indexes
for N*N chessboard
'''
if(x >= 0 and y >= 0 and x < n and y < n and board[x][y] == -1):
return True
return False
def printSolution(n, board):
'''
A utility function to print Chessboard matrix
'''
for i in range(n):
for j in range(n):
print(board[i][j], end=' ')
print()
def solveKT(n):
'''
This function solves the Knight Tour problem using
Backtracking. This function mainly uses solveKTUtil()
to solve the problem. It returns false if no complete
tour is possible, otherwise return true and prints the
tour.
Please note that there may be more than one solutions,
this function prints one of the feasible solutions.
'''
# Initialization of Board matrix
board = [[-1 for i in range(n)]for i in range(n)]
# move_x and move_y define next move of Knight.
# move_x is for next value of x coordinate
# move_y is for next value of y coordinate
move_x = [2, 1, -1, -2, -2, -1, 1, 2]
move_y = [1, 2, 2, 1, -1, -2, -2, -1]
# Since the Knight is initially at the first block
board[0][0] = 0
# Step counter for knight's position
pos = 1
# Checking if solution exists or not
if(not solveKTUtil(n, board, 0, 0, move_x, move_y, pos)):
print("Solution does not exist")
else:
printSolution(n, board)
def solveKTUtil(n, board, curr_x, curr_y, move_x, move_y, pos):
'''
A recursive utility function to solve Knight Tour
problem
'''
if(pos == n**2):
return True
# Try all next moves from the current coordinate x, y
for i in range(8):
new_x = curr_x + move_x[i]
new_y = curr_y + move_y[i]
if(isSafe(new_x, new_y, board)):
board[new_x][new_y] = pos
if(solveKTUtil(n, board, new_x, new_y, move_x, move_y, pos+1)):
return True
# Backtracking
board[new_x][new_y] = -1
return False
# Driver Code
#if __name__ == "__main__":
# Function Call
#solveKT(n)
# printing main program process id
# creating processes
p1 = multiprocessing.Process(target=solveKT(n))
p2 = multiprocessing.Process(target=solveKT(n))
# starting processes
p1.start()
p2.start()
# wait until processes are finished
p1.join()
p2.join()
# end time
end = time.time()
# total time taken
print(f"Runtime of the program is {end - start}")
# This code is contributed by AAKASH PAL
它打印出的时间是 61.015116691589355 秒。似乎它解决了第一个问题(p1),然后解决了第二个问题。当我尝试在没有多处理的情况下解决时,所花费的时间仅为 25.836514472961426 秒。那么,多处理真的可以独立处理多任务吗?
解决方案
问题在这里
p1 = multiprocessing.Process(target=solveKT(n))
p2 = multiprocessing.Process(target=solveKT(n))
函数的参数总是被急切地评估。solveKT(n)
甚至在多处理过程开始之前进行评估。
您需要告诉 multiprocessing 您想要调用solveKT(n)
而不实际调用solveKT(n)
. 您可以通过分别传递名称和参数来做到这一点:
p1 = multiprocessing.Process(target=solveKT, args=(n,))
p2 = multiprocessing.Process(target=solveKT, args=(n,))
文档:https ://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process
推荐阅读
- python - 如何从python中的字典字典中获取列表列表?
- python - 如何在 python 中使用 twitter API 抓取包含特定链接的推文?
- lua - Lua 局部函数不选择局部变量成员,除非在声明时分配
- reactjs - 为什么它不起作用,函数调用和模块导入?
- python - 最有效的带有 if 语句的 Python 循环
- c - My function aren't working after i called them
- javascript - Sqlite 等待数据库打开
- javascript - 仅检查输入数字
- flutter - Flutter StreamBuilder ListView在流数据更改时不会重新加载
- amazon-web-services - 使用 react-native-aws3 上传到 S3