python Thread.join & setDaemon sample
prototype:
join([timeout]) # wait the child thread complete
setDaemon(True) # once the daemon thread complete, the child thread complete too.
from threading import Thread
from PIL import Image
class ImageThread(Thread):
def __init__(self, filepath):
Thread.__init__(self)
self.filepath = filepath
self.openresult = 0
def run(self):
try:
im = Image.open(self.filepath)
im.show()
self.openresult = 1
except Exception, e:
print (e)
imt = ImageThread('image.gif')
imt.setDaemon(True) # should set before start
imt.start()
imt.join(10) # wait 10 seconds
join([timeout]) # wait the child thread complete
setDaemon(True) # once the daemon thread complete, the child thread complete too.
from threading import Thread
from PIL import Image
class ImageThread(Thread):
def __init__(self, filepath):
Thread.__init__(self)
self.filepath = filepath
self.openresult = 0
def run(self):
try:
im = Image.open(self.filepath)
im.show()
self.openresult = 1
except Exception, e:
print (e)
imt = ImageThread('image.gif')
imt.setDaemon(True) # should set before start
imt.start()
imt.join(10) # wait 10 seconds
Comments
Post a Comment