From 61787fecea4e0058dc65d5dcb29afe3399621ce0 Mon Sep 17 00:00:00 2001 From: Mazz Mosley Date: Thu, 16 Jul 2015 14:32:39 +0100 Subject: [PATCH] Resolve race condition Sometimes, some messages were being executed at the same time, meaning that the status wasn't being overwritten, it was displaying on a separate line for both doing and done messages. Rather than trying to have both sets of statuses being written out concurrently, we write out all of the doing messages first. Then the done messages are written out/updated, as they are completed. Signed-off-by: Mazz Mosley --- compose/utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/compose/utils.py b/compose/utils.py index 3efde052..5ffe7b70 100644 --- a/compose/utils.py +++ b/compose/utils.py @@ -21,8 +21,10 @@ def parallel_execute(command, containers, doing_msg, done_msg, **options): stream = codecs.getwriter('utf-8')(sys.stdout) lines = [] - def container_command_execute(container, command, **options): + for container in containers: write_out_msg(stream, lines, container.name, doing_msg) + + def container_command_execute(container, command, **options): return getattr(container, command)(**options) with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: @@ -41,6 +43,10 @@ def parallel_execute(command, containers, doing_msg, done_msg, **options): def write_out_msg(stream, lines, container_name, msg): + """ + Using special ANSI code characters we can write out the msg over the top of + a previous status message, if it exists. + """ if container_name in lines: position = lines.index(container_name) diff = len(lines) - position @@ -56,6 +62,8 @@ def write_out_msg(stream, lines, container_name, msg): lines.append(container_name) stream.write("{}: {}... \r\n".format(container_name, msg)) + stream.flush() + def json_hash(obj): dump = json.dumps(obj, sort_keys=True, separators=(',', ':'))