首页 > 解决方案 > python中的问题试图打开2个终端

问题描述

我正在尝试创建一个允许您同时打开 2 个终端的 python 脚本,它工作正常,但是如果我尝试将它插入到循环中,则第二个终端不会打开并且它会跳到下一条语句,这里是工作正常的代码:

    from subprocess import Popen, PIPE, CREATE_NEW_CONSOLE
    import multiprocessing as mp
    import threading as th

    def run(command):
            cmd = Popen(command, PIPE, creationflags=CREATE_NEW_CONSOLE)
            com, err = cmd.communicate()
            print(com,err)

    if __name__ == "__main__":
        mp.freeze_support()
        command = f"python testFile.py"  # or command = f"testFile.exe"
        process = mp.Process(target=run, args= (command,))
        process.start()
        input("Wait ")

正如我所说,这很好用,但是如果我尝试将其插入循环中,则第二个终端不会打开:

    from subprocess import Popen, PIPE, CREATE_NEW_CONSOLE
    import multiprocessing as mp
    import threading as th

    def run(command):
        cmd = Popen(command, PIPE, creationflags=CREATE_NEW_CONSOLE)
        com, err = cmd.communicate()
        print(com,err)

    while True:
        if __name__ == "__main__":
            mp.freeze_support()
            command = f"python testFile.py"  # or command = f"testFile.exe"
            process = mp.Process(target=run, args= (command,))
            process.start()
            input("Wait ")

标签: pythonpython-3.xmultithreadingconsolemultiprocessing

解决方案


推荐阅读