spacepaste

  1.  
  2. from Queue import Queue
  3. from multiprocessing import Process
  4. def returning_wrapper(func, *args, **kwargs):
  5. kwargs.get("multiprocess_returnable").put(func(*args, **kwargs))
  6. class Multiprocess(object):
  7. """Cute decorator to run a function in multiple processes."""
  8. def __init__(self, func):
  9. self.func = func
  10. self.processes = []
  11. def __call__(self, *args, **kwargs):
  12. num_processes = kwargs.get("multiprocess_num_processes", 2) # default to two processes.
  13. return_obj = kwargs.get("multiprocess_returnable", Queue()) # default to stdlib Queue
  14. for i in xrange(num_processes):
  15. pro = Process(target=returning_wrapper, args=tuple([self.func] + list(args)), kwargs=kwargs)
  16. self.processes.append(pro)
  17. pro.start()
  18. def __eq__(self, other):
  19. # Fuck you pickle
  20. return True
  21. @Multiprocess
  22. def info():
  23. print title
  24. print 'module name:', __name__
  25. print 'parent process:', os.getppid()
  26. print 'process id:', os.getpid()
  27. info()
  28.