0

I have the following code that has 2 variables running 2 separate pools to process a bunch of tables and the progress should be reflected via the tqdm bar. I belive I have accomplished that but my problem now is there are new lines of progress bars with 0 progress and I can't figure out where it's coming from. What am I doing wrong?


CODE

from multiprocessing import Pool, Manager
from tqdm import *
from collections import deque

pbar = tqdm(position=0)


def task(tables_started, right_list, left_list, current_table, process):
    if current_table not in tables_started:
        tables_started.append(current_table)
        if process == "left_process":
            left_list.append(current_table)
        else:
            right_list.append(current_table)
        # print(f"Starting {current_table} running on {process}.")
        return True
    else:
        return False


def return_callback(result):
    # global pbar
    # print(result)
    if result:
        # print("updating")
        pbar.update()
    else:
        # print("not updating")
        pass


if __name__ == '__main__':
    right_tables = deque()
    manager = Manager()
    for i in range(10):
        right_tables.append("Table" + str(i))

    left_tables = right_tables.copy()
    left_tables.reverse()
    # print(left_tables)
    # print(right_tables)

    tables_started = manager.list()
    right_list = manager.list()
    left_list = manager.list()

    left_pool = Pool(processes=2)
    right_pool = Pool(processes=2)
    print(f"left_tables: {left_tables}")
    print(f"right_tables: {right_tables}")
    pbar.total = len(left_tables)
    while left_tables:
        left_table = left_tables.pop()
        left_res = left_pool.apply_async(task, (tables_started, right_list, left_list, left_table, "left_process"), callback=return_callback)
        right_table = right_tables.pop()
        right_res = right_pool.apply_async(task, (tables_started, right_list, left_list, right_table, "right_process"), callback=return_callback)
    
    left_pool.close()
    right_pool.close()

    left_pool.join()
    right_pool.join()

    # print(f"right_list: {right_list}")
    # print(f"left_list: {left_list}")

RESULT terminal screenshot of result

bakaneko
  • 1
  • 2
  • There are a lot of [`tqdm` & `multiprocessing` questions](https://stackoverflow.com/search?q=tqdm+multiprocessing): have you checked them? – Timus Jan 23 '23 at 11:53
  • @Timus - was able to find where I was doing it wrong. Thanks! – bakaneko Jan 31 '23 at 07:46

1 Answers1

0

Found out what was causing it. I declared the pbar as global and it seems in each call/declaration of pbar, it will print in the terminal. I removed the global declaration of pbar and moved it inside the main. The following code worked:

from multiprocessing import Pool, Manager, freeze_support
from tqdm import *
from collections import deque

def work(tables_started, right_list, left_list, current_table, process):
    if current_table not in tables_started:
        tables_started.append(current_table)
        if process == "left_process":
            left_list.append(current_table)
        else:
            right_list.append(current_table)
        # print(f"Starting {current_table} running on {process}.")
        print("updated")
        return True
    else:
        print("didn't update")
        return False


def return_callback(result):
    # global pbar
    # print(result)
    if result:
        # print("updating")
        pbar.update()
            # pbar.close()
    else:
        # print("not updating")
        pass


if __name__ == '__main__':
    freeze_support()
    right_tables = deque()
    manager = Manager()

    for i in range(10):
        right_tables.append("Table" + str(i))

    left_tables = right_tables.copy()
    left_tables.reverse()
    # print(left_tables)
    # print(right_tables)

    tables_started = manager.list()
    right_list = manager.list()
    left_list = manager.list()

    left_pool = Pool(processes=2)
    right_pool = Pool(processes=2)
    print(f"left_tables: {left_tables}")
    print(f"right_tables: {right_tables}")
    pbar = tqdm(total = len(left_tables), position=0)
    while left_tables:
        left_table = left_tables.pop()
        left_res = left_pool.apply_async(work, (tables_started, right_list, left_list, left_table, "left_process"), callback=return_callback)
        right_table = right_tables.pop()
        right_res = left_pool.apply_async(work, (tables_started, right_list, left_list, right_table, "right_process"), callback=return_callback)

    left_pool.close()
    right_pool.close()
    left_pool.join()
    right_pool.join()

    # print(f"right_list: {right_list}")
    # print(f"left_list: {left_list}")
bakaneko
  • 1
  • 2