Skip to content

Commit

Permalink
Merge pull request #66 from rolobio/workers-arg
Browse files Browse the repository at this point in the history
Adding workers argument back in.
  • Loading branch information
rolobio committed Apr 9, 2015
2 parents 2af67eb + c84cb1c commit e2442b6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion sshm/_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/env python3

# This is the official version of sshm
__version__ = '2.0.1'
__version__ = '2.0.2'

__long_description__ = '''
SSH Multi v%s. SSH into multiple machines at once.
Expand Down
8 changes: 6 additions & 2 deletions sshm/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

__all__ = ['sshm', 'uri_expansion']
disable_formatting = False
default_workers = 20


# This is used to parse a range string
Expand Down Expand Up @@ -237,7 +238,7 @@ def ssh(thread_num, context, uri, command, extra_arguments, if_stdin=False):

CHUNK_SIZE = 65536

def sshm(servers, command, extra_arguments=None, stdin=None, disable_formatting_var=False, max_workers=5):
def sshm(servers, command, extra_arguments=None, stdin=None, disable_formatting_var=False, workers=default_workers):
"""
SSH into multiple servers and execute "command". Pass stdin to these ssh
handles.
Expand Down Expand Up @@ -265,6 +266,9 @@ def sshm(servers, command, extra_arguments=None, stdin=None, disable_formatting_
instance.
@type stdin: file
@param workers: The max amount of concurrent SSH connections.
@type workers: int
@returns: A list containing (success, handle, message) from each method
call.
"""
Expand Down Expand Up @@ -308,7 +312,7 @@ def sshm(servers, command, extra_arguments=None, stdin=None, disable_formatting_
next_uri = next(uri_gen)
while next_uri or threads:
# Start a new thread if there are any URIs left
while next_uri and len(threads) < max_workers:
while next_uri and len(threads) < workers:
thread = threading.Thread(target=ssh, args=(thread_num, context,
next_uri, command, extra_arguments, if_stdin))
thread.start()
Expand Down
2 changes: 2 additions & 0 deletions sshm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def get_argparse_args(args=None):
help='Disable command formatting.')
parser.add_argument('-u', '--quiet', action='store_true', default=False,
help="Hide SSHM's server information on output (this implies sorted).")
parser.add_argument('-w', '--workers', type=int, default=20,
help="Limit the amount of concurrent SSH connections.")
parser.add_argument('--version', action='version', version='%(prog)s '+__version__)
args, extra_args = parser.parse_known_args(args=args)

Expand Down
8 changes: 8 additions & 0 deletions sshm/test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def test_get_argparse_args(self):
self.assertEqual(command, 'ls')
self.assertEqual(extra_args, [])

# You can specify the amount of workers
provided = ['example.com', 'ls', '-w 5']
args, command, extra_args = get_argparse_args(provided)
self.assertEqual(args.servers, ['example.com',])
self.assertEqual(command, 'ls')
self.assertEqual(args.workers, 5)
self.assertEqual(extra_args, [])


def test__print_handling_newlines(self):
"""
Expand Down

0 comments on commit e2442b6

Please sign in to comment.