Work with external processes

I worked on a GUI application that should manage external processes. Working with external processes leads to many problems that can make life difficult for a programmer. I feel that maintaining this application takes an unacceptably long time. I am trying to list what makes it difficult to work with external processes so that I can come up with ways to alleviate pain. This look turned into a rant, which, as I thought, I would post here to get some feedback and give some guidance to anyone who is thinking about heading into these very dark waters. Here is what I have so far:

  • 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():
        # Can't move the window until process completes
        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, , .

, , , , . , ? ?

+3
1

. . - .

. , , , . , , - , , .

, , , - pid. , , pid , pid . pid, , . pid, .

, , , .

+2

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


All Articles