How can I show progress for parallely tasks nicely. /u/MrMrsPotts Python Education

Take this code:

from math import factorial from decimal import Decimal, getcontext from joblib import Parallel, delayed from tqdm import trange import time def calc(n_digits, pos, total): # 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, position=pos, desc=f"Job {pos + 1} of {total}", leave=False): 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 = 3 # Define the tasks (e.g., compute first 100, 200, 300, 400 digits of pi) tasks = [1200, 1700, 900, 1400] # Edit to make code for longer # Run tasks in parallel results = Parallel(n_jobs=n_cores)(delayed(calc)(n, pos, len(tasks)) for (pos, n) in enumerate(tasks)) if __name__ == "__main__": parallel_with_joblib() 

If I run it, it has some problems that stop it looking good. First after one of the jobs is finished it leaves a blank line above Job 4 or 4. Or sometimes it makes a copy of one of the lines so I get two copies of Job 2 of 4. Is it possible to fix this and also just leave a completed line as it is? I don’t mind using something other than tqdm if that would work bettter.

submitted by /u/MrMrsPotts
[link] [comments]

​r/learnpython Take this code: from math import factorial from decimal import Decimal, getcontext from joblib import Parallel, delayed from tqdm import trange import time def calc(n_digits, pos, total): # 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, position=pos, desc=f”Job {pos + 1} of {total}”, leave=False): 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 = 3 # Define the tasks (e.g., compute first 100, 200, 300, 400 digits of pi) tasks = [1200, 1700, 900, 1400] # Edit to make code for longer # Run tasks in parallel results = Parallel(n_jobs=n_cores)(delayed(calc)(n, pos, len(tasks)) for (pos, n) in enumerate(tasks)) if __name__ == “__main__”: parallel_with_joblib() If I run it, it has some problems that stop it looking good. First after one of the jobs is finished it leaves a blank line above Job 4 or 4. Or sometimes it makes a copy of one of the lines so I get two copies of Job 2 of 4. Is it possible to fix this and also just leave a completed line as it is? I don’t mind using something other than tqdm if that would work bettter. submitted by /u/MrMrsPotts [link] [comments] 

Take this code:

from math import factorial from decimal import Decimal, getcontext from joblib import Parallel, delayed from tqdm import trange import time def calc(n_digits, pos, total): # 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, position=pos, desc=f"Job {pos + 1} of {total}", leave=False): 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 = 3 # Define the tasks (e.g., compute first 100, 200, 300, 400 digits of pi) tasks = [1200, 1700, 900, 1400] # Edit to make code for longer # Run tasks in parallel results = Parallel(n_jobs=n_cores)(delayed(calc)(n, pos, len(tasks)) for (pos, n) in enumerate(tasks)) if __name__ == "__main__": parallel_with_joblib() 

If I run it, it has some problems that stop it looking good. First after one of the jobs is finished it leaves a blank line above Job 4 or 4. Or sometimes it makes a copy of one of the lines so I get two copies of Job 2 of 4. Is it possible to fix this and also just leave a completed line as it is? I don’t mind using something other than tqdm if that would work bettter.

submitted by /u/MrMrsPotts
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *