Vielen Dank nochmal für das sehr coole Beispiel.
Ich hab das ganz mal als MultiCounter umgebaut um Multithreading zu testen.
Hier der Code:
Code: Alles auswählen
#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-
import threading
import time
class Counter(threading.Thread):
def __init__(self, nr, increaseCountValue ):
threading.Thread.__init__(self)
self.count = 0
self.nr = nr
self.increaseCountValue = increaseCountValue
# Values for self.running:
# 0 : Ready.
# 1 : Running.
# 2 : Finished.
self.running = 0
def run(self):
self.running = 1
self.increaseCount()
def increaseCount(self):
self.count += self.increaseCountValue
print 'Counter ' + str(self.nr) + ' ' + str(self.count)
self.checkCount()
time.sleep(0.2)
if not self.running == 2:
self.increaseCount()
def checkCount(self):
if self.count >= 1000:
self.count = 0
class Application:
def __init__(self):
counterArray = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
print "Create and start 15 counter"
for i in range(15): # 0-14
counterArray[i] = Counter(i,i)
counterArray[i].start()
print "Main-Programm wait"
time.sleep(3)
print "Stop 15 counter"
for i in range(15):
counterArray[i].running = 2
print "Main-Program finished."
if __name__ == "__main__":
app = Application()