0

cmd run python with output redirection, when python run a start, cmd still waiting for start finish

cmd: python 1.py >> log.txt

in 1.py:

import os
os.system("start python 2.py") 

in 2.py:

import time
for i in range(50):
    time.sleep(1)
    print(i)

1.py can close, but cmd still waiting 2.py close.

how to set it so that when 1.py is closed, cmd is also finished too?

M Z
  • 4,571
  • 2
  • 13
  • 27
  • 1
    This is an [XY problem](https://xyproblem.info/) caused by not reading the Python documentation for [os.system](https://docs.python.org/3/library/os.html#os.system) which clearly describes that this function should not be used anymore since many years in newly written Python script as it runs in background `%ComSpec% /c` expanding to `%SystemRoot%\System32\cmd.exe /c` expanding usually to `C:\Windows\System32\cmd.exe /c` and the arguments as defined in Python script in the string appended. – Mofi Apr 27 '23 at 05:11
  • `cmd.exe` uses the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) with a [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure to run other executables and gives users no control how `CreateProcess` is called by `cmd.exe`. The internal command __START__ of `cmd.exe` calls also `CreateProcess` with giving the user a bit more control over the creation process as __START__ has options to control some parameter values – Mofi Apr 27 '23 at 05:13
  • The Python documentation recommends to use the [subprocess module](https://docs.python.org/3/library/subprocess.html) which is on Windows a Python wrapper for the Windows kernel function `CreateProcess` called without or with a `STARTUPINFO` structure. The usage of `subprocess.Popen` gives a Python script writer full control over nearly all `CreateProcess` function parameters and values in the `STARTUPINFO` structure to run an executable as really needed for a task. – Mofi Apr 27 '23 at 05:19
  • `1.py` could use `subprocess.Popen` to run one more `python.exe` for execution of `2.py` with the [Process Creation Flags](https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags) `CREATE_NO_WINDOW` or `CREATE_NEW_CONSOLE` or `DETACHED_PROCESS` as separate parallel running process without or with a console window. `CreateProcess` does not search for executables like `cmd.exe` does on writing just `python` instead of the fully qualified file name (drive + path + name + extension). There must be specified the executable with its full name. – Mofi Apr 29 '23 at 18:14
  • [sys.executable](https://docs.python.org/3/library/sys.html#sys.executable) can be used in `1.py` to get the fully qualified file name of `python.exe` interpreting `1.py` to start this executable for interpreting `2.py` as separate running process and not a different python.exe on multiple Python versions are installed and the execution of `1.py` was started by the user with a specific version of Python instead of `python.exe` found by `cmd.exe` using the environment variables `PATH` and `PATHEXT` or newest installed version on user used `py.exe` without the version option. – Mofi Apr 29 '23 at 18:15

1 Answers1

0

one option is to run the command in background mode. In Linux you can do it by adding a & at end of the command.

import os
os.system("python3 2.py &")
  • This does not work on Windows. `&` at end of a Windows Command Processor command line is interpreted as unconditional __AND__ command operator as described [here](https://stackoverflow.com/a/25344009/3074564) which is ignored if there is no more command after `&`. The internal command __START__ of `cmd.exe` is needed to run a command in a separate process without (no usage of __START__ option `/B`) or with (usage of __START__ option `/B`) redirecting its output to `stdout` and `stderr` to standard output or standard error stream of `cmd.exe`. – Mofi Apr 27 '23 at 05:08