提问人:Charles 提问时间:10/12/2023 最后编辑:Charles 更新时间:10/12/2023 访问量:19
Python 使用实时覆盖输出运行多个子进程
Python Run multiple subprocesses with live, overwriting output
问:
在带有 Python 3.7.9 的 Windows 10 下,我正在使用 Python 生成多个子进程。这些输出中的每一个都有一个覆盖的实时输出,如下所示:subprocess.Popen
C:\Users\Me\MyFolder>my_command.exe
[> ] 0%
然后像这样
C:\Users\Me\MyFolder>my_command.exe
[=====> ] 50%
然后终于像这样
C:\Users\Me\MyFolder>my_command.exe
[==========>] 100%
my_command finished !
C:\Users\Me\MyFolder>
如何在 Python 中生成多个子进程,每个子进程都调用并将它们的东西输出到给定的行中,实时更新整行?my_command
喜欢这个:
C:\Users\Me\MyFolder>python call_multiple_times_my_command.py
Sub0 : [> ] 0%
Sub1 : [==========>] 100% my_command finished !
Sub2 : [=====> ] 50%
我们可以同意换行符将被空格替换,因此任何给定子进程的输出永远不会超过一行。
我的代码目前如下所示:
import subprocess
processes = []
for i in range(0,3,1):
processes.append(subprocess.Popen("my_command.exe", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False))
for process in processes:
print(process)
stdout, sterr = process.communicate()
print(stdout)
But what I see when printing `std_out` is a garbled mix of all outputs of all subprocesses.
I found the `blessings` library but it requires `curses` which seems to only be available in later Python versions.
答: 暂无答案
评论