How to use output of childprocess in called proces

2019-05-30 09:04发布

问题:

I have created two modules in python. One of the module is for creating GUI using Tkinter and second one is for capturing and storing images.When I called opencv module in Tkinter module,it runs opencv module first and after releasing camera,it was running Tkinter module.So I used subprocess.Popen(). Now I want output of subprocess into Tkinter module. The code for creating GUI using Tkinter is as follows.

import sys
from Tkinter import *
import Tkinter
import subprocess
def capcam():
    command="python2 imacap.py"
    subprocess.Popen(command,shell=True)
root=Tk()
add=Frame(root)
add.grid()
root.title("Test")
capcam()
button_height=11
button_width=29
button_ipadx=2
button_ipady=2
text_box = Entry(add,justify=RIGHT,width=100, font=100)
text_box.grid(row = 0, column = 1,columnspan = 5)
text_box.insert(0, "0")
bttn_3 = Button(add, height= button_height ,width= button_width,text = "3")
bttn_3.grid(row = 3, column = 2, padx=button_ipadx, pady=button_ipady)

Following is the code of child process.

import cv2.cv as cv
capture = cv.CaptureFromCAM(0)
num = 0
while True:
    img = cv.QueryFrame(capture)
    cv.SaveImage('pic'+str(num)+'.jpg', img)
    if num == 500:
        del(capture)
        break
    if cv.WaitKey(10) == 27:
        break
    num += 1

I want the value of num variable at runtime in mainprocess and want to pass it to entry without terminating child process.

回答1:

Hold a reference

p = subprocess.Popen(command,shell=True, stdout = subprocess.PIPE)

Then you can do

num = int(p.stdout.readline()) # blocks!

if you do

print(num) in the child process

Also have a look at the module multiprocessing. It can solve the problem in other ways.



回答2:

Can I have Tk events handled while waiting for I/O? shows how to avoid blocking while reading a subprocess output from GUI thread. Assuming imacap.py has print(num) in it:

def read_output(self, pipe, mask):
    data = os.read(pipe.fileno(), 1 << 20)
    if not data: # eof
        root.deletefilehandler(proc.stdout)
    else:
        print("got: %r" % data)

proc = Popen(["python2", "imacap.py"], stdout=PIPE, stderr=STDOUT)
root.createfilehandler(proc.stdout, READABLE, read_output)

The complete code example: tkinter-read-async-subprocess-output.py demonstrates how to read the subprocess output without threads using Tkinter. It shows the output in the GUI and stops the subprocess on a button press.

If tk.createfilehandler() doesn't work on your system; you could try to use a background thread instead. See kill-process.py for the code example.