[Python] Running functions in parallel and seeing their progress

Discussão em 'Python' iniciado por Stack, Janeiro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am using joblib to run four processes on four cores in parallel. I would like to see the progress of the four processes separately on different lines. However, what I see is the progress being written on top of each other to the same line until the first process finishes.

    from math import factorial
    from decimal import Decimal, getcontext
    from joblib import Parallel, delayed
    from tqdm import trange
    import time

    def calc(n_digits):
    # number of iterations
    n = int(n_digits+1/14.181647462725477)
    n = n if n >= 1 else 1

    # set the number of digits for our numbers
    getcontext().prec = n_digits+1

    t = Decimal(0)
    pi = Decimal(0)
    deno = Decimal(0)

    for k in trange(n):
    t = ((-1)**k)*(factorial(6*k))*(13591409+545140134*k)
    deno = factorial(3*k)*(factorial(k)**3)*(640320**(3*k))
    pi += Decimal(t)/Decimal(deno)

    pi = pi * Decimal(12) / Decimal(640320 ** Decimal(1.5))
    pi = 1/pi

    # no need to round
    return pi


    def parallel_with_joblib():
    # Define the number of cores to use
    n_cores = 4

    # Define the tasks (e.g., compute first 100, 200, 300, 400 digits of pi)
    tasks = [1000, 1500, 700, 1200]


    # Run tasks in parallel
    results = Parallel(n_jobs=n_cores)(delayed(calc)(n) for n in tasks)


    if __name__ == "__main__":
    parallel_with_joblib()


    I would also like the four lines to be labelled "Job 1 of 4", "Job 2 of 4" etc.

    Continue reading...

Compartilhe esta Página