How to start a subprocess, display its output in a graphical interface and allow its completion?

I am trying to write an application that executes subprocesses and (among other things) displays their output in a graphical interface and allows the user to click a button to cancel them. I start such processes as follows:

queue = Queue.Queue(500)
process = subprocess.Popen(
    command,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT)
iothread = threading.Thread(
    target=simple_io_thread,
    args=(process.stdout, queue))
iothread.daemon=True
iothread.start()

where simple_io_thread is defined as follows:

def simple_io_thread(pipe, queue):
    while True:
        line = pipe.readline()
        queue.put(line, block=True)
        if line=="":
            break

. "get" s . , . ( - , , .) terminate , , , I/O . - . . ( , , . , .) I/O , .

- , (< 0,5 ) , - ( ) ) .

, -. , Windows Linux Python 2.6 Tkinter GUI, .


EDIT - , , , , , , , - . , , , , . :

process.stdout.close()

:

IOError: close() called during concurrent operation on the same file object.

... . :

os.close(process.stdout.fileno())

:

close failed in file object destructor: IOError: [Errno 9] Bad file descriptor

... , .

+3
4

, , -, , subprocess.Popen io_thread, . , while True: while process.poll() == None:.

process.poll() ; , (.. process.poll() == None). if line == "": break.

, , , script, : -
IOError: close() called during concurrent operation on the same file object..

, , , () io_thread, , , (, , , ..) , , subprocess.stdout, while... ie: -

def io_thread(subprocess,logfile,lock):
    for line in subprocess.stdout:
        lock.acquire()
        print line,
        lock.release()
        logfile.write( line )

, bufsize subprocess.Popen, .

+2

, , , - - ...

, , , , ( ) , .

, () communication() , .

my_thread.join()
print my_thread.is_alive()
my_popen.communicate()
+2

, , os.close() , ?

+1

write... , , .

  • Popen stdout
  • read read simple_io_thread .

, .

queue = Queue.Queue(500)
r, w = os.pipe()
process = subprocess.Popen(
    command,
    stdout=w,
    stderr=subprocess.STDOUT)
iothread = threading.Thread(
    target=simple_io_thread,
    args=(os.fdopen(r), queue))
iothread.daemon=True
iothread.start()

os.close(w)

You can close the phone and it iothreadwill be disconnected without any exceptions.

+1
source

Source: https://habr.com/ru/post/1717836/


All Articles