【问题标题】:SLURM squeue format argument fails from subprocess.PopenSLURM squeue 格式参数从 subprocess.Popen 失败
【发布时间】:2016-12-19 21:30:14
【问题描述】:

我正在尝试从 python 脚本调用SLURM squeue。命令,

/usr/bin/squeue --Format=username,jobid,name,timeleft

在命令行中运行良好,但在 subprocess.Popen 中失败:

    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  File "/n/home00/DilithiumMatrix/.conda/envs/py35/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/n/home00/DilithiumMatrix/.conda/envs/py35/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: '/usr/bin/squeue --Format=username,jobid,name,timeleft'

MWE:

import subprocess
command = "/usr/bin/squeue --Format=username,jobid,name,timeleft"
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
text = p.stdout.read()
print(text)

/usr/bin/squeue 在命令行或Popen 都可以正常工作。

它是否会失败,因为它需要一些关于正在执行squeue 命令的用户/组的信息,并且在通过 python 运行时(不知何故)丢失了?还有什么可能导致这种情况?

【问题讨论】:

  • 您对subprocess.Popen 的呼叫是什么样的?它是如何失败的?
  • @MarkkuK。哎呀,对不起 --- 包括 MWE 和错误消息。
  • 您需要将命令作为字符串列表传递:command = ["/usr/bin/squeue", "--Format=username,jobid,name,timeleft"]
  • 该死的。我尝试了"/usr/bin/squeue --Format=username,jobid,name,timeleft"["/usr/bin/squeue", "--Format=", "username,jobid,name,timeleft"],但没有尝试过妥协案例......你知道为什么它不能正常工作吗?如果您提交了答案,我将很乐意接受您的回答!非常感谢
  • 谢谢,我提交了答案。您的第二种形式就像在命令行上调用/usr/bin/squeue,在= 之后有一个空格。

标签: python command-line subprocess jobs slurm


【解决方案1】:

subprocess.Popen 的第一个参数是字符串或字符串列表。如果它是单个字符串,它将被解释为文件名。这就是你得到错误的原因。 要传递字符串列表,它应该与 shell 将参数传递给进程的方式相匹配。一个标准的 shell 会用空格分割你的命令行,所以不要这样:

command = "/usr/bin/squeue --Format=username,jobid,name,timeleft"

你需要这个:

command = ["/usr/bin/squeue", "--Format=username,jobid,name,timeleft"]

正如您在评论中提到的那样,在“=”处拆分第二个参数只会混淆 squeue,然后会看到两个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多