Python 子进程 block

标签 python subprocess wait blocking communicate

我的模块子进程有问题;我正在从 Python 运行脚本:

subprocess.Popen('./run_pythia.sh', shell=True).communicate()

有时它只是阻塞并且没有完成执行脚本。在我使用 .wait() 之前,但我 switched to .communicate() 。尽管如此,问题仍然存在。

首先脚本编译一些文件,然后执行到一个文件中:

run_pythia.sh:

#!/bin/bash
#PBS -l walltime=1:00:00

./compile.sh
./exec > resultado.txt

compile.sh:

O=`find ./ -name "*.o" | xargs`

# LOAD cernlib2005
module load libs/cernlib/2005

# Compile and Link
FC=g77
CERNLIBPATH="-L/software/local/cernlib/2005/lib -lpacklib"

$FC call_pyth_mix.f analise_tt.f $O $CERNLIBPATH -o exec

最佳答案

您执行的脚本是否能保证run_pythia.sh完成执行?如果没有,您可能不想使用诸如 communicate() 之类的阻塞方法。您可能想研究一下与返回的进程句柄的 .stdout.stderr.stdin 文件句柄进行交互(在非阻塞方式)。

此外,如果您仍然想使用 communicate(),您需要将 subprocess.PIPE 对象传递给 Popen 的构造函数参数。

阅读documentation在模块上了解更多详细信息。

关于Python 子进程 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2769694/

相关文章:

python - 更改 Python 环境变量 PYTHONUSERBASE

python - 从 python 执行 .jar 文件

python - 编辑 : Subprocess wont open python file in new window?

multithreading - 如何以线程安全的方式停止正在运行的任务?

angularjs - 在 AngularJS 中,如何等待 $http 请求的响应?

python - 如何使用 Python 将文件夹放在 PATH 上?

python - 根据另一列的名称重命名 Pandas Multiindex

python - 从 Python 启动另一个程序>单独<

android - 如何在 android 中阻止 UI 几秒钟?

Python:插入似然函数的值范围总是返回相同的值