Search code examples
pythonpython-3.xmultithreadingpyqt5subprocess

Is there any influence of the main process when launching a subprocess with subprocess.Popen?


I'm trying to find information about "side effects" of launching Windows process with the subprocess.Popen library of Python.

I've heard that there are some good/bad practices for launching Windows commands from Python, and a big software company is blaming my software for making his crash, so I wonder if there is really any side effect.

Here is a snippet to explain my self:

def execute_command(command, env=None):
    res = subprocess.Popen(command,
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           env=env,
                           creationflags=subprocess.CREATE_NO_WINDOW)

    start = time()
    output, error = res.communicate()
    execution_time = time() - start

    if output:
        output = output.decode(_SYSTEM_ENCODING, errors='replace')
    else:
        output = ""

    if error:
        error = error.decode(_SYSTEM_ENCODING, errors='replace')
    else:
        error = ""

    return res.returncode, output, error, execution_time


def start_software():

    # do some stuff

    execute_command('"C:\Program Files\Software\Software.exe" --foo --faa ')

    # do some other stuff


# This to avoid blocking my
# pyQt5 graphical interface, because the windows command 
# may be running by days or hours
th=Threading.thread(target=start_software)
th.start()


So as far as I know, the command is launched in the same way that a user would type the command in a cmd, it is launched in a separate process and do not shares the same memory with the main process.

My snippet is included in a Python 3.7 Qt5 software, compiled with pyinstaller, and they say that since my graphical interface includes HTML, the windows library MsHTML is being used, and its causing the second software to crash...


Solution

  • This is rather speculative, but hopefully at least vaguely useful for directing your efforts.

    Process isolation if properly implemented should prevent any two processes from disturbing each other. In fact, I would predict that your problem lies elsewhere, even on the rickety platform that is Windows.

    Where your problem is somewhat more likely to lie is in conflicting use of some OS resource. The addition of threading might be contributing to that. If the GUI parts of your program are using something which isn't thread-safe under the hood, and the competing program is doing the same thing, the result could be a crash, or worse.

    Unfortunately, at this level of detail, it's impossible to offer more than wild speculation, because we don't have access to the competing program, or the parts of your program whiqh are interacting with the component which is actually crashing.