Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 生成和运行子流程时显示进度_Python_Subprocess_Progressdialog - Fatal编程技术网

Python 生成和运行子流程时显示进度

Python 生成和运行子流程时显示进度,python,subprocess,progressdialog,Python,Subprocess,Progressdialog,我需要在生成和运行子流程时显示一些进度条或其他内容。 如何使用python实现这一点 import subprocess cmd = ['python','wait.py'] p = subprocess.Popen(cmd, bufsize=1024,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p.stdin.close() outputmessage = p.stdout.read() #Th

我需要在生成和运行子流程时显示一些进度条或其他内容。 如何使用python实现这一点

import subprocess

cmd = ['python','wait.py']
p = subprocess.Popen(cmd, bufsize=1024,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.stdin.close()
outputmessage = p.stdout.read() #This will print the standard output from the spawned process
message = p.stderr.read()

我可以用这段代码生成子流程,但我需要在每一秒都过去的时候打印出一些东西。

据我所知,你所需要做的就是将这些读取放入一个带有延迟和打印的循环中-它必须是精确的一秒还是大约一秒?

据我所知,你所需要做的就是将这些读取放入一个带有延迟和打印的循环中-它必须是吗精确到一秒或大约一秒?

由于子进程调用被阻塞,因此在等待时打印某些内容的一种方法是使用多线程。下面是一个使用线程的示例。\u计时器:

import threading
import subprocess

class RepeatingTimer(threading._Timer):
    def run(self):
        while True:
            self.finished.wait(self.interval)
            if self.finished.is_set():
                return
            else:
                self.function(*self.args, **self.kwargs)


def status():
    print "I'm alive"
timer = RepeatingTimer(1.0, status)
timer.daemon = True # Allows program to exit if only the thread is alive
timer.start()

proc = subprocess.Popen([ '/bin/sleep', "5" ])
proc.wait()

timer.cancel()

另一方面,在使用多个管道时调用stdout.read()可能会导致死锁。应改用subprocess.communicate()函数。

由于子进程调用被阻塞,在等待时打印内容的一种方法是使用多线程。下面是一个使用线程的示例。\u计时器:

import threading
import subprocess

class RepeatingTimer(threading._Timer):
    def run(self):
        while True:
            self.finished.wait(self.interval)
            if self.finished.is_set():
                return
            else:
                self.function(*self.args, **self.kwargs)


def status():
    print "I'm alive"
timer = RepeatingTimer(1.0, status)
timer.daemon = True # Allows program to exit if only the thread is alive
timer.start()

proc = subprocess.Popen([ '/bin/sleep', "5" ])
proc.wait()

timer.cancel()
另一方面,在使用多个管道时调用stdout.read()可能会导致死锁。应改用subprocess.communicate()函数