-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexercice_2_(thread).py
executable file
·47 lines (41 loc) · 1.07 KB
/
exercice_2_(thread).py
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
#!/usr/bin/env python
import threading
import time
class Affiche(threading.Thread):
def __init__(self, nom = ''):
threading.Thread.__init__(self)
self.nom = nom
self.Terminated = False
def run(self):
i = 0
while not self.Terminated:
print self.nom, i
i += 1
time.sleep(2.0)
print "le thread "+self.nom +" s'est termine proprement"
def stop(self):
self.Terminated = True
class Affiche2(threading.Thread):
def __init__(self, nom = ''):
threading.Thread.__init__(self)
self.nom = nom
self._stopevent = threading.Event( )
def run(self):
i = 0
while not self._stopevent.isSet():
print self.nom, i
i += 1
self._stopevent.wait(2.0)
print "le thread "+self.nom +" s'est termine proprement"
def stop(self):
self._stopevent.set( )
a = Affiche('Thread A')
b = Affiche('Thread B')
c = Affiche2('Thread C')
a.start()
b.start()
c.start()
time.sleep(6.5)
a._Thread__stop()
b.stop()
c.stop()