Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from multiprocessing import Process, Pool
- from Queue import Queue
- from threading import Thread
- class Task(object):
- # Bunch of code you want to execute / Just examples
- def add():
- print(1+1)
- def divide()
- print(5/5)
- class ProcessWorer(Thread):
- """ Thread executing process command tasks """
- def __init__(self, tasks):
- Thread.__init__(self)
- self.tasks = tasks
- self.daemon = True
- self.start()
- def run(self, retry=True):
- while True:
- proccess = self.tasks.get(True)
- try:
- process.add()
- process.divide()
- pass
- except Exception as e:
- print e
- finally:
- #print "Finalizing task"
- self.tasks.task_done()
- class ThreadPool(num_threads):
- """ Pool of threads cosuming processes from queue """
- def __init__(self, num_threads):
- self.queue = Queue(num_threads)
- for _ in range(num_threads):
- ProcessWorer(self.queue)
- def add_task(self, process):
- """ Add a process/task to the queue """
- self.queue.put(process)
- def wait_completion(self):
- """ Wait for completion of all process """
- self.queue.join()
- if __name__ == "__main__":
- num_threads = 8 #Set the maxium number of threads your computer can run
- thread_pool = ThreadPool(num_threads)
- #process_list = [] #add whatever you want
- for i in processes_list:
- task = Task()
- thread_pool.add_task(task)
- thread_pool.wait_completion()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement