Skip to content

Instantly share code, notes, and snippets.

@varunpant
Forked from tekknolagi/lines.py
Created February 25, 2024 17:24
Show Gist options
  • Select an option

  • Save varunpant/1c67a5e4cc9aa483fa2ad2e27afc4c44 to your computer and use it in GitHub Desktop.

Select an option

Save varunpant/1c67a5e4cc9aa483fa2ad2e27afc4c44 to your computer and use it in GitHub Desktop.

Revisions

  1. @tekknolagi tekknolagi revised this gist Feb 22, 2024. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion lines.py
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,8 @@ def func(repo_name):
    log(repo_name, "Running tests")
    randsleep()
    log(repo_name, f"Result in {repo_name}.json")
    del last_output_per_process[repo_name]
    with terminal_lock:
    del last_output_per_process[repo_name]


    repos = [f"repo{letter}" for letter in string.ascii_uppercase]
  2. @tekknolagi tekknolagi revised this gist Feb 22, 2024. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion lines.py
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,6 @@ def func(repo_name):
    randsleep()
    log(repo_name, f"Result in {repo_name}.json")
    del last_output_per_process[repo_name]
    return f"{repo_name} done"


    repos = [f"repo{letter}" for letter in string.ascii_uppercase]
  3. @tekknolagi tekknolagi revised this gist Feb 22, 2024. 1 changed file with 23 additions and 12 deletions.
    35 changes: 23 additions & 12 deletions lines.py
    Original file line number Diff line number Diff line change
    @@ -2,21 +2,33 @@

    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):
    last_output_per_process[repo_name] = " ".join(str(arg) for arg in 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 _ in sorted_lines:
    print("\x1b[1A\x1b[2K", end="") # move up cursor and delete whole line
    for repo_name, last_line in sorted_lines:
    print(f"{repo_name}: {last_line}")
    fill_output()


    def randsleep():
    time.sleep(random.randint(1, 5))
    time.sleep(random.randint(1, 2))


    def func(repo_name):
    @@ -35,15 +47,14 @@ def func(repo_name):
    return f"{repo_name} done"


    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
    numprocs = multiprocessing.cpu_count()
    for _ in range(numprocs):
    print()
    with multiprocessing.Pool() as pool:
    for result in pool.imap(
    func, ["repoA", "repoB", "repoC", "repoD"], chunksize=1
    ):
    pass
    fill_output()
    with multiprocessing.Pool(num_procs) as pool:
    pool.map(func, repos, chunksize=1)
    clean_up()
  4. @tekknolagi tekknolagi revised this gist Feb 22, 2024. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions lines.py
    Original file line number Diff line number Diff line change
    @@ -38,10 +38,11 @@ def func(repo_name):
    with multiprocessing.Manager() as manager:
    last_output_per_process = manager.dict()
    terminal_lock = manager.Lock()
    numprocs = 2
    with multiprocessing.Pool(numprocs) as pool:
    for _ in range(numprocs):
    print()
    # Make space for our output
    numprocs = multiprocessing.cpu_count()
    for _ in range(numprocs):
    print()
    with multiprocessing.Pool() as pool:
    for result in pool.imap(
    func, ["repoA", "repoB", "repoC", "repoD"], chunksize=1
    ):
  5. @tekknolagi tekknolagi revised this gist Feb 22, 2024. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions lines.py
    Original file line number Diff line number Diff line change
    @@ -31,15 +31,17 @@ def func(repo_name):
    log(repo_name, "Running tests")
    randsleep()
    log(repo_name, f"Result in {repo_name}.json")
    # Note: To limit the output to the number of active processes, del from
    # last_output_per_process.
    del last_output_per_process[repo_name]
    return f"{repo_name} done"


    with multiprocessing.Manager() as manager:
    last_output_per_process = manager.dict()
    terminal_lock = manager.Lock()
    with multiprocessing.Pool(2) as pool:
    numprocs = 2
    with multiprocessing.Pool(numprocs) as pool:
    for _ in range(numprocs):
    print()
    for result in pool.imap(
    func, ["repoA", "repoB", "repoC", "repoD"], chunksize=1
    ):
  6. @tekknolagi tekknolagi created this gist Feb 22, 2024.
    46 changes: 46 additions & 0 deletions lines.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #!/usr/bin/env python

    import multiprocessing
    import random
    import time


    def log(repo_name, *args):
    last_output_per_process[repo_name] = " ".join(str(arg) for arg in args)
    with terminal_lock:
    sorted_lines = last_output_per_process.items()
    for _ in sorted_lines:
    print("\x1b[1A\x1b[2K", end="") # move up cursor and delete whole line
    for repo_name, last_line in sorted_lines:
    print(f"{repo_name}: {last_line}")


    def randsleep():
    time.sleep(random.randint(1, 5))


    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")
    # Note: To limit the output to the number of active processes, del from
    # last_output_per_process.
    return f"{repo_name} done"


    with multiprocessing.Manager() as manager:
    last_output_per_process = manager.dict()
    terminal_lock = manager.Lock()
    with multiprocessing.Pool(2) as pool:
    for result in pool.imap(
    func, ["repoA", "repoB", "repoC", "repoD"], chunksize=1
    ):
    pass