|
2 | 2 | import sys
|
3 | 3 | import logging
|
4 | 4 | import subprocess
|
| 5 | +import queue |
5 | 6 |
|
6 | 7 |
|
7 | 8 | logger = logging.getLogger(__name__)
|
8 | 9 |
|
9 | 10 |
|
10 | 11 | class Nodes(object):
|
11 |
| - def __init__(self, nodes, dry_run=False, ssh_user=None): |
| 12 | + def __init__(self, nodes, dry_run=False, ssh_user=None, queue_size=0): |
12 | 13 | assert isinstance(nodes, list)
|
13 | 14 | assert len(nodes) > 0
|
14 | 15 | assert isinstance(nodes[0], str)
|
15 | 16 | self._nodes = nodes
|
16 | 17 | self._dry_run = bool(dry_run)
|
17 | 18 | self._ssh_user = ssh_user
|
18 | 19 | self._logger = logger.getChild(self.__class__.__name__)
|
| 20 | + self._queue = queue.Queue(queue_size) |
| 21 | + self._qsize = queue_size |
19 | 22 |
|
20 | 23 | @property
|
21 | 24 | def nodes_list(self):
|
@@ -83,7 +86,23 @@ def execute_async_ret(self, cmd, check_retcode=True, nodes=None, results=None):
|
83 | 86 |
|
84 | 87 | actual_cmd = self._get_ssh_command_prefix() + [host, cmd]
|
85 | 88 | process = subprocess.Popen(actual_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
| 89 | + |
| 90 | + if self._qsize > 0: |
| 91 | + self._queue.put((actual_cmd, process, host)) |
| 92 | + if not self._queue.full(): |
| 93 | + continue |
| 94 | + if not self._queue.empty(): |
| 95 | + actual_cmd, process, host = self._queue.get() |
| 96 | + process.wait() |
| 97 | + |
86 | 98 | running_jobs.append((actual_cmd, process, host))
|
| 99 | + |
| 100 | + if self._qsize > 0: |
| 101 | + while not self._queue.empty(): |
| 102 | + actual_cmd, process, host = self._queue.get() |
| 103 | + process.wait() |
| 104 | + running_jobs.append((actual_cmd, process, host)) |
| 105 | + |
87 | 106 | return running_jobs
|
88 | 107 |
|
89 | 108 | def execute_async(self, cmd, check_retcode=True, nodes=None, results=None):
|
|
0 commit comments