使用子进程获取光伏输出

编程入门 行业动态 更新时间:2024-10-15 14:12:33
本文介绍了使用子进程获取光伏输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写一个脚本来在 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 的参数.您希望将 -p 和 restore_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=True,pv 没有接收任何参数,所以它挂了,因为它仍在等待文件名.同样,解决方案是使用 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()

更多推荐

使用子进程获取光伏输出

本文发布于:2023-08-05 02:06:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1301654.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   光伏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!