pool.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import sys
  2. import multiprocessing
  3. import warnings
  4. if sys.platform != 'win32':
  5. multiprocessing.set_start_method('fork')
  6. class CommonPool(object):
  7. def map(self, func, args):
  8. return list(map(func, args))
  9. class AutoPool(object):
  10. def __init__(self, mode, processes):
  11. if mode == 'multiprocessing' and sys.platform == 'win32':
  12. warnings.warn('multiprocessing not support in windows, turning to multithreading')
  13. mode = 'multithreading'
  14. self.mode = mode
  15. self.processes = processes
  16. if mode == 'vectorization':
  17. pass
  18. elif mode == 'cached':
  19. pass
  20. elif mode == 'multithreading':
  21. from multiprocessing.dummy import Pool as ThreadPool
  22. self.pool = ThreadPool(processes=processes)
  23. elif mode == 'multiprocessing':
  24. from multiprocessing import Pool
  25. self.pool = Pool(processes=processes)
  26. else: # common
  27. self.pool = CommonPool()
  28. def map(self, func, args):
  29. return self.pool.map(func, args)