multithreading - ReCreating threads in python -
i'm using following template recreate threads need run infinity. want know if template scalable in terms of memory. threaded destroyed properly?
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 work :(" class aworkerthread(threading.thread): def __init__(self): threading.thread.__init__(self) def run(self): time.sleep(1) print "i want 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()
the thing bother me following picture taking in eclipse show debug info, , seems thread stacking it.
i see nothing wrong image. there's main thread , 2 threads created (according code, 3 threads supposed running @ time)
like other python objects, threads garbage collected when they're not used; e.g. in main while cycle, when instantiate class (let's
alazythread
), oldthreada
value destroyed (maybe not @ point, shortly after)the main while cycle, use sleep (e.g.
time.sleep(1)
), otherwise consume processor, uselessly checking if other threads running.
Comments
Post a Comment