我目前有这个代码在上下文之后管道stdout或stderr:
def IsGoalVertexFound(graph: Graph[(VertexId,(Int,Float,Float,Float,String)),Float],goalVertex:(VertexId,(Int,Float,Float,Float))): Boolean ={
var IsgoalFound:Boolean=false
var targetVertex=graph.vertices.filter{ case (id,attr) => id == goalVertex._1 && attr._2._2!=Float.PositiveInfinity}.first()
// if(targetVertex.isEmpty())
// IsgoalFound=true
return IsgoalFound
}
然后我用def execteInstruction(cmd, outputNumber):
do_stdout = DEVNULL
do_stderr = DEVNULL
if outputNumber != 2:
do_stdout = subprocess.PIPE
else:
do_stderr = subprocess.PIPE
return subprocess.Popen(cmd, shell=True, stderr=do_stderr, stdout=do_stdout)
读取结果。这非常有效,除了我需要读取自定义fd,因为我正在使用子进程来运行在文件描述符26上输出的命令。
我见过写入另一个fd的代码而不是stdout或stderr,但没有从自定义fd读取的代码。 那么如何从subprocess.Popen管道自定义fd?
答案 0 :(得分:0)
因为我需要一个答案,所以这对其他人来说是一个迟来的答案:
要在Python中实现a similar use of additional fds,我想出了以下代码:
import os, subprocess
read_pipe, write_pipe = os.pipe()
xproc = subprocess.Popen(['Xephyr', '-displayfd', str(write_pipe)],
pass_fds=[write_pipe])
display = os.read(read_pipe, 128).strip()
我认为,在os.dup2(write_pipe, 26)
构造函数与Popen
组合之前的pass_fds=[26]
可以为您提供一个特定的fd号码,而不是仅仅选择os.pipe
的任何东西,但最后一步我还没有测试。
如果没有其他问题,应该可以将fd 26重定向到write_pipe
所导致的结果,如果您使用subprocess.Popen
调用在调用前运行exec 26>$WHATEVER
的shell脚本包装器实际的目标命令。