Python 3.x 如何为多处理python中的思路分配资源
我有一个需要通过第三方程序运行的多个模拟,因此我实现了一个python多处理脚本,使程序的多个实例同时运行模拟 我的代码具有以下结构:Python 3.x 如何为多处理python中的思路分配资源,python-3.x,multiprocessing,Python 3.x,Multiprocessing,我有一个需要通过第三方程序运行的多个模拟,因此我实现了一个python多处理脚本,使程序的多个实例同时运行模拟 我的代码具有以下结构: import multiprocessing import win32com.client NUM_LICENSES_TO_USE = 4 # number of program licenses to use. Also number of parallel processes. def run_simulation(inputs): # 1.
import multiprocessing
import win32com.client
NUM_LICENSES_TO_USE = 4 # number of program licenses to use. Also number of parallel processes.
def run_simulation(inputs):
# 1.
program_instance = win32com.client.Dispatch('TheProgram') # create an instance of the program
# creating an instance requests a license seat from the license server then launch the program
# commands are then given to the program by accessing the object built-in methods.
# 2.
program_instance.run_sim(inputs) # run the simulation
# 3.
program_instance.save_results() # save simulation results to file
# 4.
program_instance.close() # close the instance and release license seat
def run_all_simulations(all_inputs):
with multiprocessing.Pool(processes=NUM_LICENSES_TO_USE) as pool:
pool.map(run_simulation, all_inputs)
if __name__ == "__main__":
all_inputs = get_inputs_from_file()
run_all_simulations(all_inputs)
对于每次模拟,上述代码(1)请求许可证,启动程序,(2)使用该元素集的输入运行模拟(3)将结果保存到文件,以及(4)关闭程序,释放许可证
问题在于,打开和关闭程序以及从服务器请求许可证需要时间,并且会使消息传递变得混乱。如果当时有人偷偷提出许可证申请,座位就会丢失
理想情况下,我希望在开始时请求许可证,然后为每个并行进程分配一个许可证。程序\u实例将在每个并行进程开始时创建一次,并在所有\u输入中的作业队列完成后关闭
有办法做到这一点吗