【发布时间】:2020-11-02 10:56:57
【问题描述】:
我在 Windows 中使用 Visual Studio 代码编写了许多 Python 脚本,用于对 3 个生物系统进行随机模拟。我总共有 6 个 Python 脚本,每个生物系统都有一个使用 Python 的多处理库并行模拟系统 5 次的脚本和另一个依次模拟系统 5 次的脚本。
但是,在 Windows 中使用 Visual Studio Code 运行脚本会导致顺序脚本总是更快(不是我想要的)。
有人建议在 IDE 中设置并行进程的开销可能是我的问题。所以我在 Windows PowerShell 中运行脚本,但顺序脚本仍然更快。
第一个问题:在 Windows 中运行并行模拟是否有任何已知问题可以解释为什么顺序脚本始终更快?
我现在正在尝试使用适用于 Linux 的 Windows 子系统运行脚本。这些脚本使用了很多导入,包括 numpy、scipy、datetime 和 multiprocessing。只有当我在这里运行脚本时,我才会收到以下错误:
ModuleNotFoundError: No module named 'numpy'
第二个问题:如何安装所有相关模块和导入以在适用于 Linux 的 Windows 子系统中运行我的 Python 脚本。
如果有帮助,下面是顺序和并行进程的代码:
顺序:
def repeat_func(times, start_state, LHS, stoch_rate, state_change_array):
""" Function to call and run other functions multiple times """
start = datetime.utcnow()
for i in range(times):
popul_num_all, tao_all = gillespie_tau_leaping(start_state, LHS, stoch_rate, state_change_array)
end = datetime.utcnow()
sim_time = end - start
print("Simulation time:\n", sim_time)
平行:
def parallel_func(v):
gillespie_tau_leaping(start_state, LHS, stoch_rate, state_change_array)
if __name__ == '__main__':
start = datetime.utcnow()
with Pool() as p:
pool_results = p.map(parallel_func, [1, 2, 3, 4, 5])
end = datetime.utcnow()
sim_time = end - start
print("Simulation time:\n", sim_time)
干杯。
【问题讨论】:
标签: python windows parallel-processing multiprocessing windows-subsystem-for-linux