如何让python脚本区分输出是管道还是进程替换

2024-05-19 01:09:14 发布

您现在位置:Python中文网/ 问答频道 /正文

试图区分stdout是要进行管道替换还是进程替换

my_python_script.py

#!/usr/bin/python3.6
if sys.__stdin__.isatty():
    print("__stdin__ is TTY")
else:
    print("__stdin__ is not TTY")
if sys.__stdout__.isatty():
    print("__stdout__ is TTY")
else:
    print("__stdout__ is not TTY")
if sys.__stderr__.isatty():
    print("__stderr__ is TTY")
else:
    print("__stderr__ is not TTY")

if sys.stdin.isatty():
    print("stdin is TTY")
else:
    print("stdin is not TTY")
if sys.stdout.isatty():
    print("stdout is TTY")
else:
    print("stdout is not TTY")
if sys.stderr.isatty():
    print("stderr is TTY")
else:
    print("stderr is not TTY")

如果管道或流程替换,则以下输出相同

> my_python_script.py | cat

__stdin__ is TTY
__stdout__ is not TTY
__stderr__ is TTY
stdin is TTY
stdout is not TTY
stderr is TTY

> cat <(my_python_script.py)

__stdin__ is TTY
__stdout__ is not TTY
__stderr__ is TTY
stdin is TTY
stdout is not TTY
stderr is TTY

有没有办法区分python脚本中管道的输出和进程替换

多谢各位


Tags: pyif管道ismystderrstdinstdout
2条回答

以下脚本应区分两种情况:

import os
import re
import sys

def get_fds(id):
    fds = [os.path.realpath(f"/proc/{id}/fd/{i}") 
           for i in list(os.walk(f'/proc/{id}/fd'))[0][2]]
    return [re.sub('.*/', '', i) 
            for i in fds if "pipe" in i]

if  any(i == j for i in get_fds(os.getpid())
               for j in get_fds(os.getppid())):
    print("Process substitution", file=sys.stderr)
else:
    print("Normal pipe", file=sys.stderr)

流程子系统将创建一个命名管道

$ echo <(uptime)
/dev/fd/63

$ file <(uptime)
/dev/fd/63: broken symbolic link to pipe:[11474138]

所以答案是否定的

相关问题 更多 >

    热门问题