Advertisement
Guest User

python multiprocessing

a guest
Nov 13th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. from multiprocessing import Process, Pool
  2. from Queue import Queue
  3. from threading import Thread
  4. class Task(object):
  5. # Bunch of code you want to execute / Just examples
  6. def add():
  7. print(1+1)
  8. def divide()
  9. print(5/5)
  10.  
  11. class ProcessWorer(Thread):
  12. """ Thread executing process command tasks """
  13. def __init__(self, tasks):
  14. Thread.__init__(self)
  15. self.tasks = tasks
  16. self.daemon = True
  17. self.start()
  18.  
  19. def run(self, retry=True):
  20. while True:
  21. proccess = self.tasks.get(True)
  22. try:
  23. process.add()
  24. process.divide()
  25. pass
  26. except Exception as e:
  27. print e
  28. finally:
  29. #print "Finalizing task"
  30. self.tasks.task_done()
  31.  
  32. class ThreadPool(num_threads):
  33. """ Pool of threads cosuming processes from queue """
  34. def __init__(self, num_threads):
  35. self.queue = Queue(num_threads)
  36. for _ in range(num_threads):
  37. ProcessWorer(self.queue)
  38.  
  39. def add_task(self, process):
  40. """ Add a process/task to the queue """
  41. self.queue.put(process)
  42.  
  43. def wait_completion(self):
  44. """ Wait for completion of all process """
  45. self.queue.join()
  46.  
  47.  
  48. if __name__ == "__main__":
  49.  
  50. num_threads = 8 #Set the maxium number of threads your computer can run
  51. thread_pool = ThreadPool(num_threads)
  52.  
  53. #process_list = [] #add whatever you want
  54.  
  55. for i in processes_list:
  56. task = Task()
  57. thread_pool.add_task(task)
  58.  
  59. thread_pool.wait_completion()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement