How can I spawn new shells to run Python scripts from a base Python script?

后端 未结 3 703
执笔经年
执笔经年 2020-12-03 02:55

I have successfully run several Python scripts, calling them from a base script using the subprocess module:

subprocess.popen([sys.executable, \'script.py\']         


        
相关标签:
3条回答
  • 2020-12-03 03:24

    Popen already generates a sub process to handle things. You just need to redirect the output pipes. Look at the subprocess documentation, specifically the section on popen stdin, stdout and stderr redirection.

    If you don't redirect these pipes, it inherits them from the parent. Just be careful about deadlocking your processes.

    You wanted additional windows for each subprocess. This is handled as well. Look at the startupinfo section of subprocess. It explains what options to set on windows to spawn a new terminal for each subprocess. Note that it requires the use of the shell=True option.

    0 讨论(0)
  • 2020-12-03 03:33

    To open in a different console, do (tested on Windows 7 / Python 3):

    from sys import executable
    from subprocess import Popen, CREATE_NEW_CONSOLE
    
    Popen([executable, 'script.py'], creationflags=CREATE_NEW_CONSOLE)
    
    input('Enter to exit from this launcher script...')
    
    0 讨论(0)
  • 2020-12-03 03:35

    This doesn't actually answer your question. But I've had my problems with subprocess too, and pexpect turned out to be really helpful.

    0 讨论(0)
提交回复
热议问题