Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在使用pytoexeconverter将python脚本转换为exe后,进程仍在后台运行_Python 3.x - Fatal编程技术网

Python 3.x 在使用pytoexeconverter将python脚本转换为exe后,进程仍在后台运行

Python 3.x 在使用pytoexeconverter将python脚本转换为exe后,进程仍在后台运行,python-3.x,Python 3.x,有没有一种方法可以在应用程序退出后正确地终止进程?如果是,如何将其集成到python脚本中,使其在退出时自动完成任务?我得到了这段代码,可以在应用程序崩溃时重新启动应用程序,但由于进程仍在后台运行,因此无法工作。干杯 process='app' def checkIfProcessRunning(进程): ''' 检查是否有任何正在运行的进程包含给定名称processName。 ''' #迭代所有正在运行的进程 对于psutil.process_iter()中的进程: 尝试: #检查进程名称是否

有没有一种方法可以在应用程序退出后正确地终止进程?如果是,如何将其集成到python脚本中,使其在退出时自动完成任务?我得到了这段代码,可以在应用程序崩溃时重新启动应用程序,但由于进程仍在后台运行,因此无法工作。干杯

process='app'
def checkIfProcessRunning(进程):
'''
检查是否有任何正在运行的进程包含给定名称processName。
'''
#迭代所有正在运行的进程
对于psutil.process_iter()中的进程:
尝试:
#检查进程名称是否包含给定的名称字符串。
如果proc.name().lower()中的process.lower():
返回真值
除了(psutil.NoSuchProcess、psutil.AccessDenied、psutil.ZombieProcess):
通过
返回错误
def ex():
操作系统('TASKKILL/F/IM Trailling.exe')
#atexit.寄存器(ex)
def start_进程():
返回操作系统(cmd)
尝试:
启动进程()
atexit.寄存器(ex)
尽管如此:
如果checkIfProcessRunning('process'):
打印(“进程正在运行”)
时间。睡眠(5)
其他:
打印(“进程未运行”)
启动进程()
例外情况除外,如e:
打印(e)

在程序的任何退出点,只需添加一个函数即可终止进程。您已经在检查该进程是否正在运行,并且可以使用相同的接口终止该进程

我建议将
checkIfProcessRunning
修改为,而不是返回
True
False
,如果进程存在,则返回该进程,否则返回
None

def checkIfProcessRunning(process):
    '''
    Check if there is any running process that contains the given name processName.
    '''
    #Iterate over the all the running process
    for proc in psutil.process_iter():
        try:
            # Check if process name contains the given name string.
            if process.lower() in proc.name().lower():
                return proc
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return None
如果检查(默认情况下,任何非None对象注册为truthy),这仍然适用于您的
,并且它使使用进程的
.kill()
方法构建kill方法更加容易:

def killProcessIfRunning(process):
    '''
    Kill the process, if it's running.
    '''
    proc = checkIfProcessRunning(process)
    if proc:
        proc.kill()
        # alternatively, use proc.terminate() if you want to be more hardcore and are not on Windows
        # or proc.send_signal() with SIGSTOP or something
然后,只需在程序的任何出口点调用kill方法(因为您使用了
atexit
,所以不管怎样,这都应该是
ex()
方法):


关于我在上一个回答中评论的错误,好消息是,当您将一个工作应用程序放在正确的文件夹中并取出双精度“”时,重新启动脚本可以工作,并使用以下脚本无限期地重新启动程序:

import time
import psutil
import os
app = 'Raiseexception'
cmd = r"C:\Users\g\Desktop\API\Raiseexception.exe"
def checkIfProcessRunning(app):
    '''
    Check if there is any running process that contains the given name processName.
    '''
    #Iterate over the all the running process
    for proc in psutil.process_iter():
        try:
            # Check if process name contains the given name string.
            if app.lower() in proc.name().lower():
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return False

def ex():
    os.system('TASKKILL /F /IM Raiseexception.exe')


def start_process():
    return os.startfile(cmd)

try:
    start_process()
    while True:

        if checkIfProcessRunning(app): #check for double ' '
            print("Process is running")
            time.sleep(5)

        else:
            print("Process is not running")
            start_process()

except Exception as e:
    print(e)
干杯


Lrd

所以我按照你的建议做了,在程序的退出点调用kill方法,它成功了。但是,当我使用代码的原始部分启动程序并检查它是否在spyder中运行时,它会打开程序的exe文件,一旦退出,它仍会告诉我进程正在运行。在任务管理器中,当它运行此脚本时,我有Windows命令处理器,它是可部署的>C:\Windowns\system32\cmd.exe,Console Windows主机,Raiseexception,Raiseexception(用于测试代码的应用程序的名称,两次)。当程序退出时,脚本告诉我“程序仍在运行”即使它已退出,我也看不到任何正在运行的可部署的Windows命令处理器,在其中我可以看到Raiseexception。以下是原始脚本现在的样子:导入时间导入系统导入psutil导入操作系统导入atexit导入子进程app='Raiseexception'cmd=r“C:\Users\g\Desktop\API\Raiseexception.exe”def checkIfProcessRunning(应用程序):“”“检查是否有任何正在运行的进程包含给定名称processName。”“”#在psutil.process_iter()中迭代所有正在运行的进程:try:#检查进程名称是否包含给定的名称字符串。如果proc.name().lower()中的app.lower():返回True Exception(psutil.NoSuchProcess、psutil.AccessDenied、psutil.ZombieProcess):传递返回Falsedef ex():os.system('TASKKILL/F/IM Raiseexception.exe')#atexit.register(ex)def start_process():返回os.system(cmd)#或#process=subprocess.Popen(cmd,stdout=subprocess.PIPE,creationflags=0x08000000)#process.wait()#子流程不适合我
import time
import psutil
import os
app = 'Raiseexception'
cmd = r"C:\Users\g\Desktop\API\Raiseexception.exe"
def checkIfProcessRunning(app):
    '''
    Check if there is any running process that contains the given name processName.
    '''
    #Iterate over the all the running process
    for proc in psutil.process_iter():
        try:
            # Check if process name contains the given name string.
            if app.lower() in proc.name().lower():
                return True
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
            pass
    return False

def ex():
    os.system('TASKKILL /F /IM Raiseexception.exe')


def start_process():
    return os.startfile(cmd)

try:
    start_process()
    while True:

        if checkIfProcessRunning(app): #check for double ' '
            print("Process is running")
            time.sleep(5)

        else:
            print("Process is not running")
            start_process()

except Exception as e:
    print(e)