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

Comments

Popular posts from this blog

How to fix error : no module named sendgrid when try to use sendgrid python lib in PHP.

react-native run-android : sun.security.provider.cert path.SunCertPathBuilderException : unable to find valid certification path to req uested target

react-native run-android : do not build/update modified code(App.js)