运行 SLURM 中的程序时如何保存打印语句?
How do I save print statements when running a program in SLURM?
我正在 运行 宁 Python 代码,其中包含通过 SLURM 的打印语句。通常,当我直接通过 "python program.py" 运行 Python 代码时,打印语句会出现在终端中。当我通过 SLURM 运行 我的程序时,正如预期的那样,打印语句不会出现在终端中。如何将打印语句保存到文件中,以便在程序 运行ning 时检查它们?下面是我通过 "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,需要'flushed'强制立即写入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.
您可以使用:
python -u program.py > test2.out
并且您的所有输出都将保存到 test2.out 文件。
你可以放
sys.stdout.flush()
打印感兴趣的报表后。或者自 python 3.3
print("hey", flush=True)
是一个选项。如果您频繁打印,使用无缓冲 IO(使用 python -u
)可能会降低性能,尤其是在共享文件系统上。
以上解决方案没有为我生成实时输出。 (您需要刷新打印并同步文件更改。)这是一个代码,您可以简单地添加到您的 python 脚本中。
#!/usr/bin/env python3.7
import sys
import builtins
import os
sys.stdout = open("stdout.txt", "w", buffering=1)
def print(text):
builtins.print(text)
os.fsync(sys.stdout)
print("This is immediately written to stdout.txt")
就是这样。
说明
重定向标准输出并在每个换行符后写入文件缓冲区。
sys.stdout = open("stdout.txt", "w", buffering=1)
覆盖打印功能并在操作系统端同步刷新的文件。
def print(text):
builtins.print(text)
os.fsync(sys.stdout)
我正在 运行 宁 Python 代码,其中包含通过 SLURM 的打印语句。通常,当我直接通过 "python program.py" 运行 Python 代码时,打印语句会出现在终端中。当我通过 SLURM 运行 我的程序时,正如预期的那样,打印语句不会出现在终端中。如何将打印语句保存到文件中,以便在程序 运行ning 时检查它们?下面是我通过 "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,需要'flushed'强制立即写入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.
您可以使用:
python -u program.py > test2.out
并且您的所有输出都将保存到 test2.out 文件。
你可以放
sys.stdout.flush()
打印感兴趣的报表后。或者自 python 3.3
print("hey", flush=True)
是一个选项。如果您频繁打印,使用无缓冲 IO(使用 python -u
)可能会降低性能,尤其是在共享文件系统上。
以上解决方案没有为我生成实时输出。 (您需要刷新打印并同步文件更改。)这是一个代码,您可以简单地添加到您的 python 脚本中。
#!/usr/bin/env python3.7
import sys
import builtins
import os
sys.stdout = open("stdout.txt", "w", buffering=1)
def print(text):
builtins.print(text)
os.fsync(sys.stdout)
print("This is immediately written to stdout.txt")
就是这样。
说明
重定向标准输出并在每个换行符后写入文件缓冲区。
sys.stdout = open("stdout.txt", "w", buffering=1)
覆盖打印功能并在操作系统端同步刷新的文件。
def print(text):
builtins.print(text)
os.fsync(sys.stdout)