pool.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import absolute_import
  2. from concurrent.futures import ProcessPoolExecutor
  3. import logging
  4. import six
  5. log = logging.getLogger(__name__)
  6. class PoolMetaclass(type):
  7. __instances__ = dict()
  8. __blacklist__ = ('Pool', 'PoolMetaclass')
  9. def __new__(cls, name, bases, attrs):
  10. if name in cls.__blacklist__:
  11. return super(PoolMetaclass, cls).__new__(cls, name,
  12. bases, attrs)
  13. pool_key = attrs.get('pool_key') or '{}Pool'.format(name)
  14. new_class = super(PoolMetaclass, cls).__new__(cls, pool_key,
  15. bases, attrs)
  16. setattr(new_class, 'pool_key', pool_key)
  17. if pool_key not in cls.__instances__:
  18. cls.__instances__[pool_key] = new_class
  19. return cls.__instances__[pool_key]
  20. class Pool(six.with_metaclass(PoolMetaclass, ProcessPoolExecutor)):
  21. pool_key = None
  22. def __init__(self, pool_key=None, max_workers=5):
  23. super(Pool, self).__init__(max_workers)