Rich with multiprocessing.Pool.starmap() ? #884
-
I have used the following code but it doesn't work as expected; the progress bar only moves at all after all the processes are done with the computation. with Progress() as progress:
task_id = progress.add_task("[cyan]Completed...", total=len(comb))
with multiprocessing.Pool() as pool:
miaos = pool.starmap(do_kmeans, kmeans_params)
for miao in miaos:
progress.advance(task_id) I tried to use some code similar to this, however the difference is I need to use Does anyone know how to make this work? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
I think the problem is that starmap returns a list which mean it has to wait for all the results before returning. Try pool.starmap(do_kmeans, kmeans_params, callback=lambda: progress.advance(task_id)) |
Beta Was this translation helpful? Give feedback.
-
It's unlikely to be an issue with Rich. Could your If after debugging you still think the issue may be the Progress bars, try to produce a working piece of code you can paste here. Substitute your |
Beta Was this translation helpful? Give feedback.
-
I was mistaken about that callback. I thought it would be called for each result, but it seems to be called at the end with all results. You can use import time
import multiprocessing
from rich.progress import Progress
def do_kmeans(param1):
time.sleep(5)
return param1
def call_do_kmeans(params):
return do_kmeans(*params)
if __name__ == "__main__":
algo_params = []
results = []
for x in range(100):
algo_params.append([x])
with Progress() as progress:
task_id = progress.add_task("[cyan]Working...", total=len(algo_params))
with multiprocessing.Pool(processes=5) as pool:
for result in pool.imap(call_do_kmeans, algo_params):
print(result)
results.append(result)
progress.advance(task_id)
print(results) |
Beta Was this translation helpful? Give feedback.
I was mistaken about that callback. I thought it would be called for each result, but it seems to be called at the end with all results.
You can use
imap
which yields each result as they are calculated. The following works for me: