The output from the child can be mixed with the output from the parent. This can make both conclusions erroneous and difficult to read. It’s hard to say where. It is harder to understand what happens when everything is asynchronous. Here's a far-fetched example:
import textwrap, os, time
from subprocess import Popen
test_path = 'test_file.py'
with open(test_path, 'w') as file:
file.write(textwrap.dedent('''
import time
for i in range(3):
print 'Hello %i' % i
time.sleep(1)'''))
proc = Popen('python -B "%s"' % test_path)
for i in range(3):
print 'Hello %i' % i
time.sleep(1)
os.remove(test_path)
Conclusion:
Hello 0
Hello 0
Hello 1
Hello 1
Hello 2
Hello 2
I think I could write a child process to a file. But it can be frustrating to open a file every time I want to see the result of a print statement. A.
If I have code for a child process, I could add a label, something like print 'child: Hello %i', but it can be annoying to do this for every print. And that adds some noise to the exit. And, of course, I cannot do this if I do not have access to the code.
I could manually control the output of the process. But then you open a huge can of worms with threads and polls and the like.
, , , . , . , GUI-. ...
, gui .
import textwrap, sys, os
from subprocess import Popen
from PyQt4.QtGui import *
from PyQt4.QtCore import *
test_path = 'test_file.py'
with open(test_path, 'w') as file:
file.write(textwrap.dedent('''
import time
for i in range(3):
print 'Hello %i' % i
time.sleep(1)'''))
app = QApplication(sys.argv)
button = QPushButton('Launch process')
def launch_proc():
proc = Popen('python -B "%s"' % test_path)
proc.communicate()
button.connect(button, SIGNAL('clicked()'), launch_proc)
button.show()
app.exec_()
os.remove(test_path)
Qt QProcess, . . , . , , goto, . , , "" QProcess, , . , , ...
, . , . , , - . , - .
, . , - "cmd" Windows, , , . /k, . , .
, 3 4 : . , . , - ? , stderr? . , ...
, . , , , , , 30 , .
, , , , - .
, , , . , , .
, - , .
2 , gui .
F *** ing
. . , . ... C:/ / /'. , . , "". , , : "cmd/k" python\path 1\'\ 2\'' ".
, . .
.
, stdout. , ? , , . , .
.
, os. /k, , , CMD-, ? Unix . , Google StackOverflow, , . , .
.
. , , " " . . . : cwd, , .
, , , , . , ? ?