Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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 如果我终止进程,线程也会停止吗?_Python_Multithreading_Python 3.x_Python 3.4_Python Multithreading - Fatal编程技术网

Python 如果我终止进程,线程也会停止吗?

Python 如果我终止进程,线程也会停止吗?,python,multithreading,python-3.x,python-3.4,python-multithreading,Python,Multithreading,Python 3.x,Python 3.4,Python Multithreading,我有以下代码: #!/usr/bin/python3 import os import subprocess import time import threading class StartJar(threading.Thread): def run(self): os.system("java -jar <nazwa_appki>.jar") jarFileRun = StartJar current_location = subpro

我有以下代码:

#!/usr/bin/python3
import os
import subprocess
import time
import threading


class StartJar(threading.Thread):
    def run(self):
        os.system("java -jar <nazwa_appki>.jar")

jarFileRun = StartJar        
current_location = subprocess.getoutput("pwd")

while True:
    x = subprocess.getstatusoutput("git checkout master && git reset --hard && git fetch && git pull")
    if x[0] is 0:
        os.system("git checkout master && git reset --hard && git fetch && git pull")
        os.system("mvn clean package ~/your/path")

        try:
            process_pid = subprocess.check_output(['pgrep', '-f', 'tu_podaj_nazwe_procesu']).decode()
        except subprocess.CalledProcessError as e:
            print("pgrep failed because ({}):".format(e.returncode), e.output.decode())
        else:
            try:
                os.kill(int(process_pid), 9)
                print('Process with ' + process_pid + ' PID was killed.')
            except ProcessLookupError as e:
                print("Process were old...")
            except ValueError as e:
                print("There is no such process!")

        os.system("cp ~/your/path" + ' ' + current_location)
        jarFileRun.start()
    else:
        print('No changes was made...')

time.sleep(1800)
#/usr/bin/python3
导入操作系统
导入子流程
导入时间
导入线程
类StartJar(threading.Thread):
def运行(自):
system(“java-jar.jar”)
jarFileRun=StartJar
当前位置=子流程getoutput(“pwd”)
尽管如此:
x=子流程.getstatusoutput(“git签出主控和git重置--hard&&git fetch&&git pull”)
如果x[0]为0:
系统(“git签出主控和git重置--hard&&git-fetch&&git-pull”)
系统(“mvn干净包~/your/path”)
尝试:
进程\u pid=子进程。检查\u输出(['pgrep','-f','tu\u podaj\u nazwe\u procesu'])。解码()
除subprocess.CalledProcessError为e外:
打印(“pgrep失败,因为({}):”。格式(e.returncode),e.output.decode()
其他:
尝试:
os.kill(int(进程_-pid),9)
打印('带有'+Process\u pid+'pid'的进程已终止')
除ProcessLookupError为e外:
打印(“流程已过时…”)
除ValueError为e外:
打印(“没有这样的流程!”)
系统(“cp~/your/path”+“”+当前位置)
jarFileRun.start()
其他:
打印('未进行任何更改…')
时间。睡眠(1800)
我想知道若我杀死线程运行的进程,它也会关闭吗?如果没有,我如何终止线程,以便能够再次运行它,并对我要执行的文件进行新的更改?
我试图在google中找到某个停止线程的东西,但当我将它添加到while语句的第一行时,它对我无效。

如果终止进程,则与该进程对应的所有线程都将终止。如果终止进程中的一个线程,进程仍将运行。

否它不会终止,因为值
daemon
默认为False,这意味着主进程完成时线程不会停止,它是完全独立的。但是,如果将线程的守护进程值设置为
True
,则只要主进程也在运行,它就会一直运行

为此,您可以删除
StartJar
类并定义
jarFileRun
像这样:

jarFileRun = threading.Thread(target=os.system, args=("java -jar <nazwa_appki>.jar",), daemon=True)
jarFileRun=threading.Thread(target=os.system,args=(“java-jar.jar”),daemon=True)

或者在类中为
守护进程

Yes创建一个init定义。胎面在进程内运行,如果进程死亡,那么进程内的线程也会死亡。但这很容易测试您自己。为什么您要将java作为线程运行,为什么不将其作为后台进程运行?我可以将其添加到run函数中,还是应该添加init?