Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
Multithreading 在python中重新创建线程_Multithreading_Python 2.7 - Fatal编程技术网

Multithreading 在python中重新创建线程

Multithreading 在python中重新创建线程,multithreading,python-2.7,Multithreading,Python 2.7,我正在使用下面的模板来重新创建我需要无限运行的线程。 我想知道这个模板在内存方面是否是可伸缩的。线程是否被正确地销毁 import threading import time class aLazyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): time.sleep(10) print "I

我正在使用下面的模板来重新创建我需要无限运行的线程。 我想知道这个模板在内存方面是否是可伸缩的。线程是否被正确地销毁

import threading
import time

class aLazyThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        time.sleep(10)
        print "I don not want to work :("

class aWorkerThread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        time.sleep(1)
        print "I want to work!!!!!!!"

threadA = aLazyThread()
threadA.start()
threadB = aWorkerThread()
threadB.start()

while True:

    if not (threadA.isAlive()):
        threadA = aLazyThread()
        threadA.start()

    if not (threadB.isAlive()):
        threadB = aWorkerThread()
        threadB.start()
困扰我的是下面在eclipse中拍摄的显示调试信息的图片,看起来线程正在堆叠它

  • 我看不出这个图像有什么问题。有主线程和您创建的2个线程(根据代码,3个线程应该随时运行)

  • 与任何其他Python对象一样,线程在不使用时会被垃圾收集;e、 g.在主while循环中,当实例化该类时(比如说
    aLazyThread
    ),旧的
    threadA
    值被销毁(可能不是在该点上,但不久之后)

  • 主while循环也可以使用睡眠(例如
    time.sleep(1)
    ),否则它将消耗处理器,无用地检查其他线程是否正在运行