Skip to content

Commit

Permalink
Sleep briefly while waiting for pending results to reduce CPU churn
Browse files Browse the repository at this point in the history
  • Loading branch information
jimi-c committed Oct 6, 2016
1 parent 087fb42 commit e26bce5
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/ansible/plugins/strategy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,19 @@ def __init__(self):

_sentinel = object()
def results_thread_main(strategy):
#print("RESULT THREAD STARTING: %s" % threading.current_thread())
while True:
try:
result = strategy._final_q.get()
if type(result) == object:
break
else:
#print("result in thread is: %s" % result._result)
strategy._results_lock.acquire()
strategy._results.append(result)
strategy._results_lock.release()
except (IOError, EOFError):
break
except Queue.Empty:
pass
#print("RESULT THREAD EXITED: %s" % threading.current_thread())

class StrategyBase:

Expand Down Expand Up @@ -121,7 +118,7 @@ def __init__(self, tqm):
self._results = deque()
self._results_lock = threading.Condition(threading.Lock())

#print("creating thread for strategy %s" % id(self))
# create the result processing thread for reading results in the background
self._results_thread = threading.Thread(target=results_thread_main, args=(self,))
self._results_thread.daemon = True
self._results_thread.start()
Expand Down Expand Up @@ -316,7 +313,6 @@ def parent_handler_match(target_handler, handler_name):
continue

if original_task.register:
#print("^ REGISTERING RESULT %s" % original_task.register)
if original_task.run_once:
host_list = [host for host in self._inventory.get_hosts(iterator._play.hosts) if host.name not in self._tqm._unreachable_hosts]
else:
Expand Down Expand Up @@ -533,6 +529,8 @@ def _wait_on_pending_results(self, iterator):

results = self._process_pending_results(iterator)
ret_results.extend(results)
if self._pending_results > 0:
time.sleep(0.001)

display.debug("no more pending results, returning what we have")

Expand Down

0 comments on commit e26bce5

Please sign in to comment.