Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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_Python Multiprocessing - Fatal编程技术网

Python 守护进程行为多处理多线程

Python 守护进程行为多处理多线程,python,python-multiprocessing,Python,Python Multiprocessing,为什么脚本会消亡,除非我添加while True:sleep in main?thread1和thread2都有永远运行的函数 daemon=False,即使父进程已经结束,也应该使子进程和该子进程中的子线程保持活动状态吗 EDIT1工作代码查看方法a或方法B上的注释,运行时注释块为其中一个部分: from multiprocessing import Process from threading import Thread def main(): thread1 = myclass

为什么脚本会消亡,除非我添加while True:sleep in main?thread1和thread2都有永远运行的函数 daemon=False,即使父进程已经结束,也应该使子进程和该子进程中的子线程保持活动状态吗

EDIT1工作代码查看方法a或方法B上的注释,运行时注释块为其中一个部分:

from multiprocessing import Process
from threading import Thread

def main():

    thread1 = myclass(varA)   #subclassed threading.Thread
    thread1.daemon = False
    thread1.start()

    thread2 = Thread(target=myfunc)
    thread2.daemon = False
    thread2.start()

    while True:
        time.sleep(1)


if __name__ == "__main__":

    SUBPROCESS = Process(target=main)
    SUBPROCESS.daemon = False
    SUBPROCESS.start()

这是,;多处理进程在关闭前未加入非守护进程线程。Python 3.7.0中的行为已经改变,因此进程现在加入了它们的非守护进程线程。

发布我们可以实际运行的代码,该代码在运行时实际会复制错误。另外,不要同时用python-3.x和python-2.7来标记你的问题;如果您的问题是特定于版本的,请为您实际使用的版本选择标签。如果您的问题不是特定于版本的,请不要使用版本标记。代码在while True循环中运行良好,我想知道,如果我删除主子进程中的while True循环,它为什么会在父进程终止时消失。哇,谢谢,我将在python 3.7中尝试这一点。除了stackoverflow,你有没有什么地方可以推荐你发布这些新手问题?老实说,我发现stackoverflow对于试图自学这门语言的新手来说很难。@duarte:我希望我知道一个可以推荐的地方。
from multiprocessing import Process
from threading import Thread
import time


varA = "XYZ"

class myclass(Thread):

    def __init__(self, varA):
        Thread.__init__(self)
        self.varA = varA

    def run(self):
        while True:
            print("myClass prints %s" %self.varA)


def myfunc():
    while True:
        print("myfunc prints")



def main():

    thread1 = myclass(varA)   #subclassed threading.Thread
    thread1.daemon = False
    thread1.start()

    thread2 = Thread(target=myfunc)
    thread2.daemon = False
    thread2.start()


if __name__ == "__main__":

    #METHOD A: Running as subprocess = subthreads in that subprocess will die
    SUBPROCESS = Process(target=main)
    SUBPROCESS.daemon = False
    SUBPROCESS.start()


    #METHOD B: Running as main process = subthreads in that subprocess wont die as desired
    thread1 = myclass(varA)   #subclassed threading.Thread
    thread1.daemon = False
    thread1.start()

    thread2 = Thread(target=myfunc)
    thread2.daemon = False
    thread2.start()