我需要一些控制子流程的帮助.

目前,我正在使用python控制软件的运行,如下面的代码所示.

import subprocess

# Declaring some input and output names for the third-party program
label = ['fname1', 'fname2', 'fname3', 'fname4']
input_list = [ 'inp.'+ l for l in label ]
output_list = [ 'out.'+ l for l in label ]

# Run all sets of input and output using the third-party software
for in, out in zip(input_list, output_list):
    #The bash command to run the executable
    command = f"mpirun pw.x < {in} > {out}" 
    subprocess.run(command, shell=True)

我的计算机有64个逻辑核,正如我用软件测试的那样,使用32和64不会改变计算速度,因此,我想编辑代码以适应两个并发子进程.跑mpirun -n 32 ...码.

我不知道如何进行并发操作,比如排队和控制在给定时间允许运行多少子流程实例.

请问哪个模块/库可以帮助我完成这项工作?当然,非常感谢您提供一个代码示例.

另外,PBS/SLURM系统不是一个选项,因为我还在python脚本中做一些处理工作.

谢谢

推荐答案

如果要并行运行mpirun -n 32次,请try 以下方法:

import concurrent.futures

labels = ['fname1', 'fname2']
with concurrent.futures.ProcessPoolExecutor(max_workers=len(labels)) as pool:
    for label in labels:
        command = f"mpirun -n 32 pw.x < inp.{label} > out.{label}"
        pool.submit(subprocess.run, command, shell=True)

with语句将在最后关闭池,这使它等待所有作业(job)完成.

Python-3.x相关问答推荐

使用Polars阅读按日期键分区的最新S3镶木地板文件

Python避免捕获特定异常

Pandas -我们如何在一行中应用多个要求

Pandas 插入的速度太慢了.对于跟踪代码,什么是更快的替代方案?

无法使用xpath关闭selenium中的弹出窗口

为什么我无法在django中按月筛选事件?

msg-seviri l1.5本机文件

如何使用正则表达式通过反向搜索从链接中获取特定文本

我可以设置树视图层次 struct 按钮吗?

「Python Pandas」多级索引列和行匹配,如果列和行名称相似,则排除这些单元格中的值添加

python3,将整数转换为字节:对于小整数使用 to_bytes() 有哪些替代方法?

如何在pyspark的列中按连续1分组并保持具有特定大小的组

使用 from re findall 组合连续匹配并分离非连续匹配

从日志(log)文件中查找延迟最低的用户

Python:获取未绑定的类方法

`pyspark mllib` 与 `pyspark ml` 包

python 3集合中的Discard()和Remove()函数有什么区别

Python的max函数有多高效

为什么变量 = 对象不像变量 = 数字那样工作

调用 Python doctest 时如何启用省略号?