在SLURM中运行程序时如何保存打印语句?

我正在运行一个包含通过SLURM打印语句的Python代码.通常当我通过“python program.py”直接运行Python代码时,print语句出现在终端中.当我通过SLURM运行我的程序时,正如预期的那样,打印语句不会出现在终端中.如何将打印语句保存到文件中,以便在程序运行时检查它们?以下是我通过“sbatch submit.sh”提交的提交脚本.请注意,我已经尝试了两种方法将输出写入test1.out或test2.out.请让我知道我哪里出错了!

#!/bin/bash

#SBATCH -J mysubmission
#SBATCH -p New
#SBATCH -n 1
#SBATCH -t 23:59:00
#SBATCH -o test1.out

module load gnu python

python program.py > test2.out

解决方法:

默认情况下,Python中的print是缓冲的,这意味着它不会立即写入文件或stdout,并且需要“刷新”以强制立即写入stdout.

有关可用选项,请参阅此question.

最简单的选择是使用-u选项启动Python解释器.

从python手册页:

-u Force stdin, stdout and stderr to be totally unbuffered. On systems where it matters, also put stdin, stdout and stderr in binary mode.
Note that there is internal buffering in xreadlines(), readlines() and
file-object iterators (“for line in sys.stdin”) which is not
influenced by this option. To work around this, you will want to use
“sys.stdin.readline()” inside a “while 1:” loop.

上一篇:linux – `watch`命令,带有管道`|`[复制]


下一篇:powershell-修改Job密码