I have some code:
red = "\033[1;31m"
green = "\033[1;32m"
yellow = "\033[1;33m"
blue = "\033[1;34m"
defaultcolor = "\033[0m"
class watek(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
x=1
def timer(stopon):
timertime = 0
while True:
time.sleep(1)
timertime += 1
print timertime
if timertime == stopon:
killpro()
def killpro():
sys.exit()
threadsyy = []
threadsamount = 300
i = 1
while i <= threadsamount:
thread = watek()
threadsyy.append(thread)
i += 1
print(yellow + "Thread number" + defaultcolor + ": " + red + str(i) + yellow + " created." + '\n')
a = 0
for f in threadsyy:
f.start()
a += 1
#print "Thread work " + str(a)
timer(5)
And i need to terminate the scipt after 5 seconds. I tried to use sys.exit and killing the process using psutil. Anyone know how to terminate it? I'm trying with:
class watek(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self._kill = threading.Event()
and use
watek.kill()
but it doesn't work either.
xbecomes1when you dox=1. There's nothing holding the thread alive. Try doingf.isAlive()in your for loop, you'll see that the threads are dead? Also, there's no need to tag this post with bothpython-3andpython-2, they both live under the same tag aspython- meaning they'll get the same attention, it just helps us clarify which type of code we're looking at / the challenges the two different versions come with. And since you're doingprint timertimeit indicates Python2, so i removedpython-3.