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 64 65 66 67
| #!/usr/bin/python
# thread management
import threading,Queue
from time import *
# Management of thread: thread can be stopped quickly
#http://python.developpez.com/faq/?page=Thread#ThreadKill
class myThread(threading.Thread):
def __init__(self, name=None, addressList=["78"]):
threading.Thread.__init__(self, target=self.run, name=name )
self.name=name
self._stopevent = threading.Event( )
self.addressList = addressList
self.start()
#self.addressList = addressList
def run(self):
while not self._stopevent.isSet():
print "name : ", self.name
self._stopevent.wait(1.0)
def stop(self):
self._stopevent.set( )
class access:
def __init__(self, toto):
self.doc = toto
def start(self):
self.nodequeue={}
self.nodethread={}
self.nodequeue["essai"]= Queue.Queue(0)
self.nodethread["essai"]= myThread("FORTHREAD1")
#for key in self.nodethread.keys():
# self.nodethread[key].stop()
def stop(self):
# 1) delete all queues
for item in self.nodequeue:
del item
self.nodequeue.clear()
# 2) delete all thread
for key in self.nodethread.keys():
self.nodethread[key].stop()
print
self.nodethread.clear()
def stopOneNode(self, name):
del self.nodequeue[name]
self.nodethread[name].stop()
del self.nodethread[name]
if __name__ == "__main__":
a = access('F:\\octopussy\\input\\access.xml')
a.start()
"""sleep(5.0)
a.stopOneNode("com2")"""
a.nodequeue["FORTHREAD1"].put("stop")
#menu = ihm.getchar()
menu = raw_input("entrer touche : ")
while menu != "e":
print "attente"
if menu == "s":
a.stop()
menu = raw_input("entrer touche : ")
#menu = ihm.getchar()
"""sleep(5.0)
a.stop()""" |