使用子进程获取光伏输出 [英] Getting pv output with subprocess

查看:44
本文介绍了使用子进程获取光伏输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来在 MySQL 中自动导入数据库.我正在尝试编写代码,在导入数据库时​​显示 pv 的输出:

I am writing a script to automate database imports in MySQL. I'm trying to write code that displays the output of pv as the database is imported:

    pv = subprocess.Popen(
        ["pv", "-f", restore_filepath],
        bufsize=1,
        stderr=subprocess.PIPE,
        stdout=subprocess.PIPE,
        universal_newlines=True,
        shell=True,
    )
    subprocess.Popen(
        [
            "mysql",
            "-u{}".format(db_user),
            "-p{}".format(db_pass),
            db_name,
        ],
        shell=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.DEVNULL,
        stdin=pv.stdout,
    )
    for line in pv.stderr:
        print("hello")
        sys.stdout.write("\r" + line.rstrip("\n"))
        sys.stdout.flush()

这是基于这个问题中的代码,但是它不适合我.hello 甚至永远不会被打印,即使我注释掉 for 循环中的其他行 - for line in pv.stderr 是阻塞的,我不知道为什么.它永远不会解除阻塞,所以即使进程完成,程序仍然卡住 - 我必须杀死它.

This is based on the code in this question, but it is not working for me. The hello never even gets printed, even if I comment out the other lines in the for loop - for line in pv.stderr is blocking and I don't know why. It never unblocks, so even once the process has completed, the program is still stuck - I have to kill it.

我做错了什么导致 for line in pv.stderr 阻塞?

What am I doing wrong that is causing for line in pv.stderr to block?

推荐答案

当shell=True时,args[0]是要执行的命令,args[1:]是传递给 sh 的参数.您希望将 -prestore_filepath 传递给 pv 而不是 sh.所以使用shell=False.其他 subprocess.Popen 调用也是如此.

When shell=True, args[0] is the command to be executed and args[1:] are arguments passed to sh. You want -p and restore_filepath to be passed to pv not sh. So use shell=False. The same goes for your other subprocess.Popen call.

由于 shell=Truepv 没有接收任何参数,所以它挂了,因为它仍在等待文件名.同样,解决方案是使用 shell=False.

Since with shell=True,pv is not receiving any arguments, so it is hanging because it was still waiting for a filename. So again, the solution is to use shell=False.

另请注意,当更换外壳管道时,您应该在第一个进程上关闭 stdout 以允许它在第二个进程存在时接收 SIGPIPE.这允许某些程序(处理 SIGPIPE 的程序)在管道中断时更优雅地退出.

Also note when replacing a shell pipeline, you should close stdout on the first process to allow it to receive SIGPIPE if the second process exists. This allows some programs (those that handle SIGPIPE) to more gracefully exit if the pipeline is broken.

pv = subprocess.Popen(
    ["pv", "-f", restore_filepath],
    shell=False,  # optional, since this is the default
    bufsize=1,
    stderr=subprocess.PIPE,
    stdout=subprocess.PIPE,
    universal_newlines=True,
)
mysql = subprocess.Popen(
    ["mysql",
     "-u{}".format(db_user),
     "-p{}".format(db_pass),
     db_name],
    shell=False,
    stdout=subprocess.PIPE,
    stderr=subprocess.DEVNULL,
    stdin=pv.stdout,
)
pv.stdout.close() # Allow pv to receive a SIGPIPE if mysql exits. 
for line in pv.stderr:
    print("hello")
    sys.stdout.write("\r" + line.rstrip("\n"))
    sys.stdout.flush()

这篇关于使用子进程获取光伏输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆