Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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/6/multithreading/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 - Fatal编程技术网

Python 为什么应用程序退出后线程继续运行?

Python 为什么应用程序退出后线程继续运行?,python,multithreading,Python,Multithreading,下面是一个Python应用程序,它跨越几个线程,然后生成一个新的进程并退出: from pprint import pprint import os import random import signal import sys import threading import time class Name(object): def __init__(self, name): self.name = name class CallThreads(thread

下面是一个Python应用程序,它跨越几个线程,然后生成一个新的进程并退出:

from pprint import pprint    import os
import random
import signal
import sys 
import threading
import time


class Name(object):
    def __init__(self, name):
        self.name = name


class CallThreads(threading.Thread):
    def __init__(self, target, *args):
        self.target = target
        self.args = args
        threading.Thread.__init__(self)

    def run (self):
        self.target(*self.args)


def main(args):
    print("Hello, world!")
    letter = random.choice(['A', 'B', 'C', 'D', 'E', 'F'])
    count = 0 
    while count<3:
        count += 1
        name = Name(letter+str(count))
        t = CallThreads(provider_query, name)
        t.daemon = True
        t.start()
        time.sleep(3)
        print("------------")

    print("Time to die!")
    os.system('python restart.py')
    sys.exit(0)


def provider_query(name):
    while name.name!='':
        print(name.name)
        time.sleep(1)


def signal_handler(signal, frame):
    sys.exit()


if __name__ == '__main__':
    signal.signal(signal.SIGINT, signal_handler)
    main(sys.argv)
从pprint导入pprint导入操作系统
随机输入
输入信号
导入系统
导入线程
导入时间
类名(对象):
定义初始化(self,name):
self.name=名称
类CallThreads(threading.Thread):
定义初始化(自我、目标、*args):
self.target=目标
self.args=args
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
self.target(*self.args)
def主(args):
打印(“你好,世界!”)
字母=随机。选择(['A','B','C','D','E','F'])
计数=0

当count时,您从未进入sys.exit()调用
os.system('python restart.py')
阻塞,直到restart.py完成-但是子restart.py阻塞了另一个子restart.py,等等。。。直到你最终炸毁了进程表。您应该每隔9秒在流程表中看到另一个restart.py。

restart.py的作用是什么?你确定你看到的输出不是来自那里吗?谢谢!在另一个线程中运行
os.system
命令解决了这个问题。