python 可执行文件_如何以一般的方式使Python函数运行的可执行文件的终端输出静默?...

1586010002-jmsa.png

I want to suppress all of the terminal output produced by a function that runs executables.

I have attempted to suppress the output of a Python function by using a context manager that temporarily redefines stdout and stderr each time the function is called. This suppresses terminal output produced by print calls in the function, but it doesn't seem to work when the function calls executables that produce terminal output.

So, how could the output of executables called by Python functions be suppressed?

My code is below. I have included an example function that calls ls to try to illustrate the kind of terminal output I want to suppress (though the function I'm dealing with is different).

#!/usr/bin/env python

import os

import subprocess

import sys

def main():

print("hello")

with silence():

print("there")

print("world")

with silence():

engage_command(command = "ls")

class silence(object):

def __init__(

self,

stdout = None,

stderr = None

):

if stdout == None and stderr == None:

devnull = open(os.devnull, "w")

stdout = devnull

stderr = devnull

self._stdout = stdout or sys.stdout

self._stderr = stderr or sys.stderr

def __enter__(

self

):

self.old_stdout = sys.stdout

self.old_stderr = sys.stderr

self.old_stdout.flush()

self.old_stderr.flush()

sys.stdout = self._stdout

sys.stderr = self._stderr

def __exit__(

self,

exc_type,

exc_value,

traceback

):

self._stdout.flush()

self._stderr.flush()

sys.stdout = self.old_stdout

sys.stderr = self.old_stderr

def engage_command(

command = None

):

process = subprocess.Popen(

[command],

shell = True,

executable = "/bin/bash")

process.wait()

output, errors = process.communicate()

return output

if __name__ == "__main__":

main()

In my particular case, I'm trying to run the following function (instead of the ls function above):

with propyte.silence():

stream = pyaudio.PyAudio().open(

format = pyaudio.PyAudio().get_format_from_width(1),

channels = 1,

rate = bitrate,

output = True

)

When run, this produces output like the following:

ALSA lib pcm_dsnoop.c:606:(snd_pcm_dsnoop_open) unable to open slave

ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave

ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear

ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe

ALSA lib pcm.c:2266:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side

ALSA lib pcm_dmix.c:1029:(snd_pcm_dmix_open) unable to open slave

Cannot connect to server socket err = No such file or directory

Cannot connect to server request channel

jack server is not running or cannot be started

JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock

JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for 4294967295, skipping unlock

I want to suppress that output.

EDIT: testing a solution provided by @Matthias

#!/usr/bin/env python

import contextlib

import os

import subprocess

import sys

def main():

print("hello")

with silence():

print("there")

print("world")

with silence():

engage_command(command = "ls")

@contextlib.contextmanager

def silence():

devnull = os.open(os.devnull, os.O_WRONLY)

old_stderr = os.dup(2)

sys.stderr.flush()

os.dup2(devnull, 2)

os.close(devnull)

try:

yield

finally:

os.dup2(old_stderr, 2)

os.close(old_stderr)

def engage_command(

command = None

):

process = subprocess.Popen(

[command],

shell = True,

executable = "/bin/bash")

process.wait()

output, errors = process.communicate()

return output

if __name__ == "__main__":

main()

I have not been successful in suppressing the terminal output from the print or the ls and I'm not sure why.

解决方案

You could switch from PyAudio to the sounddevice module, which already takes care of silencing the terminal output (see #12). This is how it is done there (using CFFI):

from cffi import FFI

import os

ffi = FFI()

ffi.cdef("""

/* from stdio.h */

FILE* fopen(const char* path, const char* mode);

int fclose(FILE* fp);

FILE* stderr; /* GNU C library */

FILE* __stderrp; /* Mac OS X */

""")

try:

stdio = ffi.dlopen(None)

devnull = stdio.fopen(os.devnull.encode(), b'w')

except OSError:

return

try:

stdio.stderr = devnull

except KeyError:

try:

stdio.__stderrp = devnull

except KeyError:

stdio.fclose(devnull)

If you want a pure Python solution, you can try this context manager:

import contextlib

import os

import sys

@contextlib.contextmanager

def ignore_stderr():

devnull = os.open(os.devnull, os.O_WRONLY)

old_stderr = os.dup(2)

sys.stderr.flush()

os.dup2(devnull, 2)

os.close(devnull)

try:

yield

finally:

os.dup2(old_stderr, 2)

os.close(old_stderr)

UPDATE:

The context manager above silences the standard error output (stderr), which is used for the annoying messages from PortAudio mentioned in the original question. To get rid of the standard output (stdout), as in your updated question, you'll have to replace sys.stderr with sys.stdout and the file descriptor 2 with the number 1:

@contextlib.contextmanager

def ignore_stdout():

devnull = os.open(os.devnull, os.O_WRONLY)

old_stdout = os.dup(1)

sys.stdout.flush()

os.dup2(devnull, 1)

os.close(devnull)

try:

yield

finally:

os.dup2(old_stdout, 1)

os.close(old_stdout)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值