Parallelise scale
Signed-off-by: Mazz Mosley <mazz@houseofmnowster.com>
This commit is contained in:
parent
85c90daa18
commit
5c29ded6ac
2 changed files with 79 additions and 27 deletions
|
|
@ -12,6 +12,51 @@ from threading import Thread
|
|||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def parallel_create_execute(create_function, container_numbers, msgs={}, **options):
|
||||
"""
|
||||
Parallel container creation by calling the create_function for each new container
|
||||
number passed in.
|
||||
"""
|
||||
stream = codecs.getwriter('utf-8')(sys.stdout)
|
||||
lines = []
|
||||
errors = {}
|
||||
|
||||
for number in container_numbers:
|
||||
write_out_msg(stream, lines, number, msgs['doing'])
|
||||
|
||||
q = Queue()
|
||||
|
||||
def inner_call_function(create_function, number):
|
||||
try:
|
||||
container = create_function(number)
|
||||
except APIError as e:
|
||||
errors[number] = e.explanation
|
||||
q.put(container)
|
||||
|
||||
for number in container_numbers:
|
||||
t = Thread(
|
||||
target=inner_call_function,
|
||||
args=(create_function, number),
|
||||
kwargs=options,
|
||||
)
|
||||
t.daemon = True
|
||||
t.start()
|
||||
|
||||
done = 0
|
||||
total_to_create = len(container_numbers)
|
||||
while done < total_to_create:
|
||||
try:
|
||||
container = q.get(timeout=1)
|
||||
write_out_msg(stream, lines, container.name, msgs['done'])
|
||||
done += 1
|
||||
except Empty:
|
||||
pass
|
||||
|
||||
if errors:
|
||||
for number in errors:
|
||||
stream.write("ERROR: for {} {} \n".format(number, errors[number]))
|
||||
|
||||
|
||||
def parallel_execute(command, containers, doing_msg, done_msg, **options):
|
||||
"""
|
||||
Execute a given command upon a list of containers in parallel.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue