简体   繁体   English

如何在子程序完成运行之前终止Python程序?

[英]How to Terminate a Python program before its child is finished running?

I have a script that is supposed to run 24/7 unless interrupted. 我有一个脚本,除非被打断,否则应该以24/7运行。 This script is script A. I want script A to call Script B, and have script A exit while B is running. 该脚本是脚本A。我希望脚本A调用脚本B,并在脚本B运行时退出脚本A。 Is this possible? 这可能吗? This is what I thought would work 我认为这是可行的

#script_A.py
while(1)
    do some stuff
    do even more stuff
    if true:
        os.system("python script_B.py")
        sys.exit(0)


#script_B.py
time.sleep(some_time)
do something
os.system("python script_A.py")
sys.exit(0)

But it seems as if A doesn't actually exit until B has finished executing (which is not what I want to happen). 但是似乎A直到B完成执行才真正退出(这不是我想要发生的事情)。 Is there another way to do this? 还有另一种方法吗?

It seems you want to detach a system call to another thread. 看来您想将系统调用分离到另一个线程。

script_A.py script_A.py

import subprocess
import sys

while(1)
    do some stuff
    do even more stuff
    if true:
        pid = subprocess.Popen([sys.executable, "python script_B.py"]) # call subprocess
        sys.exit(0)

Anyway it does not seem a good practice at all. 无论如何,这似乎根本不是一个好习惯。 Why do you not try the script A listens the Process Stack and if it finds script B running then stops. 您为什么不尝试脚本A侦听进程堆栈,如果发现脚本B正在运行然后停止。 This is another example how you could do it. 这是另一个例子。

import subprocess
import sys
import psutil

while(1)
    #This sections queries the current processes running
    for proc in psutil.process_iter():
        pinfo = proc.as_dict(attrs=['pid', 'name'])
        if pinfo[ 'name' ] == "script_B.py":
            sys.exit(0)

    do some stuff
    do even more stuff
    if true:
        pid = subprocess.Popen([sys.executable, "python script_B.py"]) # call subprocess
        sys.exit(0)

What you are describing sounds a lot like a function call: 您所描述的听起来很像一个函数调用:

def doScriptB():
  # do some stuff
  # do some more stuff

def doScriptA():
  while True:
    # do some stuff
    if Your Condition:
      doScriptB()
      return

while True:
  doScriptA()

If this is insufficient for you, then you have to detach the process from you python process. 如果这对您来说还不够,那么您就必须将进程与python进程分离。 This normally involves spawning the process in the background, which is done by appending an ampersand to the command in bash: 这通常涉及在后台生成进程,这是通过在bash中的命令后面添加一个&符来完成的:

yes 'This is a background process' &

And detaching said process from the current shell, which, in a simple C program is done by forking the process twice. 并从当前shell分离所述过程,在简单的C程序中,是通过两次分叉该过程来完成的。 I don't know how to do this in python, but would bet, that there is a module for this. 我不知道如何在python中执行此操作,但可以打赌,有一个用于此的模块。

This way, when the calling python process exits, it won't terminate the spawned child, since it is now independent. 这样,当调用的python进程退出时,它不会终止生成的子进程,因为它现在是独立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM