简体   繁体   English

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

[英]If I terminate the process, the thread would also stop?

I've following code : 我下面的代码:

#!/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)

And I wonder if I kill process that my thread runs, it would close as well ? 而且我想知道是否杀死线程运行的进程,它也会关闭吗? If no how do i terminate the thread to be able to run it once more with new changes that had came out for the file I want to execute ? 如果否,我该如何终止线程以再次运行该线程,并对该我要执行的文件进行新的更改? I tried to find out in google something that stops thread but it didn't work for me when I added it to the first line of while statement. 我试图在Google中找到一些可以阻止线程的东西,但是当我将其添加到while语句的第一行时,它对我没有用。

If you terminate the process,all threads corresponding to that process would terminate. 如果终止该进程,则与该进程对应的所有线程都将终止。 the process will still run if you terminate one of the threads in process. 如果终止进程中的线程之一,则该进程仍将运行。

No it will not terminate because the value daemon is by default False and it means that the thread doesn't stop when the main process finishes, it's totally independent. 不,它不会终止,因为值daemon默认情况下为False,这意味着当主进程完成时线程不会停止,它是完全独立的。 However, if you set the thread's daemon value to True it will run as long as the main process runs too. 但是,如果将线程的守护程序值设置为True ,则只要主进程也运行,它将一直运行。

In order to do so you can delete your StartJar class and define jarFileRun like this: 为此,您可以删除StartJar类并定义jarFileRun如下所示:

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

Or make an init definition inside the class for daemon 或在daemon的类内进行init定义

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

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