简体   繁体   English

用python进行作业控制

[英]job control with python

I am trying to use python for job control. 我正在尝试使用python进行作业控制。 With bash, the bash script would look like: 使用bash,bash脚本如下所示:

i=0

for mu_Li in `seq -17 0.1 -14`  
do mkdir mu_$mu_Li
cp mc.x POSCAR BCLUST CLUST CSPECS  configuration  factor_group FBCLUST FCLUST point_group  PRIM mc_input configuration.corr  eci.in reference relax.log SCEL eci.out mu_$mu_Li
#cp ../POSCAR mu_$mu_Li\_$mu_Ni/POSCAR
 cd mu_$mu_Li
 sed -i "s/^\(Na \)[0-9-][.0-9-]*\(.*mu init\)/\1${mu_Li}\2/" mc_input

 sed -i "s/^\(Na \)[0-9-][.0-9-]*\(.*mu min\)/\1${mu_Li}\2/" mc_input

 sed -i "s/^\(Na \)[0-9-][.0-9-]*\(.*mu max\)/\1${mu_Li}\2/" mc_input

 ./mc.x 2>&1 > /dev/null  &
 cd ..

i=$((i+1))
if (( $i % 16 == 0 ))   
then 
wait
rm mu_*/MCLUST[0-9] mu_*/configuration*  mu_*/FBCLUST  mu_*/*CLUST mu_*/mc.x
fi
done
wait

The key is that I want to use all available cores to run multiple ./mc.x 2>&1 > /dev/null & and use wait in background. 关键是我想使用所有可用的内核来运行多个./mc.x 2>&1 > /dev/null &并在后台使用wait Should I just make everything into a string in python like: 我是否应该将所有内容都放入python中的字符串中,例如:

    COMMAND = r"""line1;line2;line3;etc."""
    subprocess.Popen(COMMAND, shell=True).communicate(input=None)

Here is a sketch of how to use subprocess to spawn multiple background processes and then wait for them. 这是有关如何使用subprocess进程生成多个后台进程然后等待它们的示意图。 In this example each process is just sleeping. 在此示例中,每个进程都处于休眠状态。

import subprocess
p1 = subprocess.Popen(['sleep', '1'])
p2 = subprocess.Popen(['sleep', '2'])

# Wait for all processes to complete and get list of return codes
rcs = [p.wait() for p in (p1, p2)]

For more information, see the documentation . 有关更多信息,请参见文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM