text: #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  # The MIT License (MIT)
  #
  # Copyright (c) 2016 Richard Hull
  #
  # Permission is hereby granted, free of charge, to any person obtaining a copy
  # of this software and associated documentation files (the "Software"), to deal
  # in the Software without restriction, including without limitation the rights
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  # copies of the Software, and to permit persons to whom the Software is
  # furnished to do so, subject to the following conditions:
  #
  # The above copyright notice and this permission notice shall be included in
  # all copies or substantial portions of the Software.
  #
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  # SOFTWARE.
  
  # Adapted from http://code.activestate.com/recipes/577187-python-thread-pool/
  # Attribution: Created by Emilio Monti on Sun, 11 Apr 2010 (MIT License).
  
  from threading import Thread
  
  
  class worker(Thread):
      """
      Thread executing tasks from a given tasks queue
      """
      def __init__(self, tasks):
          Thread.__init__(self)
          self.tasks = tasks
          self.daemon = True
          self.start()
  
      def run(self):
          while True:
              func, args, kargs = self.tasks.get()
              try:
                  func(*args, **kargs)
              except Exception as e:
                  print(e)
              self.tasks.task_done()
  
  
  class threadpool:
      """
      Pool of threads consuming tasks from a queue
      """
      def __init__(self, num_threads):
          try:
              from Queue import Queue
          except ImportError:
              from queue import Queue
  
          self.tasks = Queue(num_threads)
          for _ in range(num_threads):
              worker(self.tasks)
  
      def add_task(self, func, *args, **kargs):
          """
          Add a task to the queue
          """
          self.tasks.put((func, args, kargs))
  
      def wait_completion(self):
          """
          Wait for completion of all the tasks in the queue
          """
          self.tasks.join()
  