Skip to content

Instantly share code, notes, and snippets.

@varunpant
Forked from tekknolagi/lines.py
Created February 25, 2024 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varunpant/1c67a5e4cc9aa483fa2ad2e27afc4c44 to your computer and use it in GitHub Desktop.
Save varunpant/1c67a5e4cc9aa483fa2ad2e27afc4c44 to your computer and use it in GitHub Desktop.
#!/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