I want to update a progress bar from inside a spawned process as follows:
import multiprocessing as mp
import random
import time
from tqdm import tqdm
def test(queue, pbar, lock):
    while True:
        x = queue.get()
        if x is None:
            break
        for i in range(x):
            time.sleep(1)
            lock.acquire()
            pbar.update(1)
            lock.release()
queue = mp.Queue()
lock = mp.Lock()
processes = []
pbar = tqdm(total=5050)
for rank in range(4):
    p = mp.Process(target=test, args=(queue, pbar, lock))
    p.start()
    processes.append(p)
pbar.close()
for idx in range(100):
    queue.put(idx)
for _ in range(4):
    queue.put(None)  # sentinel values to signal subprocesses to exit
for p in processes:
        p.join()  # wait for all subprocesses to finish
The above gives inconsistent updates (progess goes up and down).
I found this answer, but none of them work for me because I want to update the progress bar inside the test function. How can I do this?
 
    