Don't use threads when max_workers=1

This commit is contained in:
Ross McFarland
2017-03-28 13:26:02 -07:00
parent fe1d1b22d7
commit 32a7b23923
2 changed files with 24 additions and 4 deletions
+19 -4
View File
@@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function, \
unicode_literals
from StringIO import StringIO
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import Future, ThreadPoolExecutor
from importlib import import_module
from os import environ
import logging
@@ -37,10 +37,21 @@ class _AggregateTarget(object):
return True
class MainThreadExecutor(object):
def submit(self, func, *args, **kwargs):
future = Future()
try:
future.set_result(func(*args, **kwargs))
except Exception as e:
future.set_exception(e)
return future
class Manager(object):
log = logging.getLogger('Manager')
def __init__(self, config_file):
def __init__(self, config_file, max_workers=None):
self.log.info('__init__: config_file=%s', config_file)
# Read our config file
@@ -48,8 +59,12 @@ class Manager(object):
self.config = safe_load(fh, enforce_order=False)
manager_config = self.config.get('manager', {})
max_workers = manager_config.get('max_workers', 4)
self._executor = ThreadPoolExecutor(max_workers=max_workers)
max_workers = manager_config.get('max_workers', 1) \
if max_workers is None else max_workers
if max_workers > 1:
self._executor = ThreadPoolExecutor(max_workers=max_workers)
else:
self._executor = MainThreadExecutor()
self.log.debug('__init__: configuring providers')
self.providers = {}
+5
View File
@@ -115,6 +115,11 @@ class TestManager(TestCase):
.sync(dry_run=False, force=True)
self.assertEquals(19, tc)
# Again with max_workers = 1
tc = Manager(get_config_filename('simple.yaml'), max_workers=1) \
.sync(dry_run=False, force=True)
self.assertEquals(19, tc)
def test_eligible_targets(self):
with TemporaryDirectory() as tmpdir:
environ['YAML_TMP_DIR'] = tmpdir.dirname