2017-04-06 59 views
1

我想从另一个python程序启动一个python程序并同时退出前一个程序。Python,启动一个.py程序并终止以前的程序

这里我做了什么:

if(self.start_button.pression==1): 
     os.system('python program_to_launch.py') 
     sys.exit() 

结果是很明显的,该计划推出的“program_to_launch.py​​”和等待,直到它完成运行。

问题在于刚刚启动的程序中的while循环。 它是无限期的持续时间。

这是问题所在;以前的方案还在等待新的计划结束后,也不会执行了`sys.exit()

我会杀了第一个程序using`

注:

method provided by this answer ,我无法导入程序启动,因为我想终止第一个程序

import program_to_launch 

所以我认为这不是一个解决方案。 和命令在链接的回答表明exec file()

execfile('program_to_launch.py')

,它提供问题在program_to_launch.py​​执行导入多个模块

回答

0

我想你可以在一个新的启动程序线程或进程:

from os import system 
from threading import Thread 
from sys import exit 

if self.start_button.pression == 1: 
    thread = Thread() 
    thread.run = lambda: system('python program_to_launch.py') 
    thread.start() 

    exit() 
+0

不幸的是,前面的程序保持打开并冻结,我使用函数print('bookmark')'来查看程序到达执行的位置,它在'exit ()'功能。至少当我关闭第二个程序时,第一个程序也会关闭它。 – Ziru93

+0

注意:我也试过'sys.exit()' – Ziru93

+0

而不是os.system试试subprocess.Popen。它应该工作,即使没有线程 – seenorth