-
-
Save varunpant/1c67a5e4cc9aa483fa2ad2e27afc4c44 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import multiprocessing | |
import random | |
import string | |
import time | |
def fill_output(): | |
to_fill = num_lines - len(last_output_per_process) | |
for _ in range(to_fill): | |
print() | |
def clean_up(): | |
for _ in range(num_lines): | |
print("\x1b[1A\x1b[2K", end="") # move up cursor and delete whole line | |
def log(repo_name, *args): | |
with terminal_lock: | |
last_output_per_process[repo_name] = " ".join(str(arg) for arg in args) | |
clean_up() | |
sorted_lines = last_output_per_process.items() | |
for repo_name, last_line in sorted_lines: | |
print(f"{repo_name}: {last_line}") | |
fill_output() | |
def randsleep(): | |
time.sleep(random.randint(1, 2)) | |
def func(repo_name): | |
log(repo_name, "Starting") | |
randsleep() | |
log(repo_name, "Installing") | |
randsleep() | |
log(repo_name, "Building") | |
randsleep() | |
log(repo_name, "Instrumenting") | |
randsleep() | |
log(repo_name, "Running tests") | |
randsleep() | |
log(repo_name, f"Result in {repo_name}.json") | |
with terminal_lock: | |
del last_output_per_process[repo_name] | |
repos = [f"repo{letter}" for letter in string.ascii_uppercase] | |
num_procs = multiprocessing.cpu_count() | |
num_lines = min(len(repos), num_procs) | |
with multiprocessing.Manager() as manager: | |
last_output_per_process = manager.dict() | |
terminal_lock = manager.Lock() | |
# Make space for our output | |
fill_output() | |
with multiprocessing.Pool(num_procs) as pool: | |
pool.map(func, repos, chunksize=1) | |
clean_up() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment