1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import threading
import time
##############################################################################
class Monthread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.encore = True # =variable pour arrêter le thread sur demande
self.enpause = False # variable pour mettre le thread en pause
self.c = 0 # simple compteur pour affichage
def run(self):
while self.encore:
while self.enpause:
time.sleep(0.5)
# activité du thread
# ...
self.c += 1
print self.c
# ...
def pause(self):
self.enpause = True
def reprise(self):
self.enpause = False
def stop(self):
self.encore = False
##############################################################################
app=Monthread()
app.start()
print u"thread lancé"
tps = time.time()
while app.isAlive():
if time.time()-tps > 1: # pause du thread au bout de 1 secondes
app.pause()
print u"thread en pause"
break
while app.isAlive():
if time.time()-tps > 2: # reprise du thread au bout de 2 secondes
print u"reprise du thread"
app.reprise()
break
while app.isAlive():
if time.time()-tps > 3: # arrêt du thread au bout de 3 secondes
app.stop()
print u"arrêt du thread"
break
print "fin" |
Partager