I'm trying to incorporate a progress bar into a piece of code that parallelizes operations using os.fork. I have tried with a rich progress bar but due to the way progress updates are handled across processes it is not working, i.e. the progress bar update within the child process, is not reflected in the parent process.
My code looks something like this:
from rich.progress import Progress
import os
with Progress() as progress:
task = progress.add_task(total=len(args_list))
for balanced_batch in even_batches_it:
if os.fork():
# do something
else:
try:
for my_tuple in balanced_batch:
do_something(my_tuple)
progress.advance(task, advance=len(balanced_genes_batch))
except Exception as exception:
# do something
Any feedback/advice is highly appreciated!
In my experience, it's best to just create a python file specifically dedicated for doing this task. It makes things a lot easier whenever I want to add a progress bar to my code, since
threadingdoesn't really work.I created some code so that when you input a function to it, it runs the function alongside a progress bar:
This code is a little inefficient since it uses
exec(). However, it is very powerful! By addingupdate(progress, task)to each line at the end of the function you want to run, the progress bar updates each line of the function. Here's how you would implement your code:Save this example code as a file, named whatever (ex
progress_bar.py). Now import the functionrun()from the python file. And now you can run your code:Last thing to note is that your code is actually structured for a loop, so you don't need my function to run it (as per the documentation), but this code may be more useful to you in the future, given that your current progress bar isn't working properly.