【问题标题】:How could I close tqdm_gui?我怎么能关闭 tqdm_gui?
【发布时间】:2019-08-07 12:26:32
【问题描述】:

我是并行处理的新手。在我的软件中,我想并行执行一些解析器作业以加快速度,并且我想通知用户解析作业仍在运行。我找到了 tqdm 来完成这项工作,我现在遇到了从 tqdm_gui 关闭图形的问题。

环顾四周,我发现了 Dan Shiebler 的这篇文章: http://danshiebler.com/2016-09-14-parallel-progress-bar/

我稍微更改了这段代码,以便将它与 tqdm 的 GUI 一起使用。见下面的代码sn-p。

如果我再次调用parallel_process_gui,在(新)图形后面仍然有一个(旧)图形。我怎样才能关闭这个,两个或所有数字?

我已经尝试过将离开标志从 True 更改为 False

kwargs = {
            'total': len(futures),
            'unit': 'it',
            'unit_scale': True,
            'leave': True,
            'desc': desc
        }

并尝试关闭图形

tqdm.tqdm_gui.close()

没有运气

代码sn-p:


import tqdm
from concurrent.futures import ProcessPoolExecutor, as_completed
from time import sleep

def parallel_process_gui(array,
                         function,
                         n_jobs=16,
                         desc='process',
                         use_kwargs=False,
                         front_num=3):
    """
        A parallel version of the map function with a progress bar.

        http://danshiebler.com/2016-09-14-parallel-progress-bar/

        Args:
            array (array-like): An array to iterate over.
            function (function): A python function to apply to the elements of array
            n_jobs (int, default=16): The number of cores to use
            use_kwargs (boolean, default=False): Whether to consider the elements
            of array as dictionaries of keyword arguments to function
            front_num (int, default=3): The number of iterations to run serially
            before kicking off the parallel job. Useful for catching bugs

        Returns:
            [function(array[0]), function(array[1]), ...]
    """
    # We run the first few iterations serially to catch bugs
    if front_num > 0:
        front = [function(**a) if use_kwargs else function(a)
                 for a in array[:front_num]]
    else:
        front = []
    # If we set n_jobs to 1, just run a list comprehension. This is useful for
    # benchmarking and debugging.
    if n_jobs == 1:
        return front + [function(**a) if use_kwargs else function(a)
                        for a in tqdm.tqdm_gui(array[front_num:])]
    # Assemble the workers
    with ProcessPoolExecutor(max_workers=n_jobs) as pool:
        # Pass the elements of array into function
        if use_kwargs:
            futures = [pool.submit(function, **a) for a in array[front_num:]]
        else:
            futures = [pool.submit(function, a) for a in array[front_num:]]
        kwargs = {
            'total': len(futures),
            'unit': 'it',
            'unit_scale': True,
            'leave': True,
            'desc': desc
        }
        # Print out the progress as tasks complete
        for f in tqdm.tqdm_gui(as_completed(futures), **kwargs):
            pass

    out = []
    # Get the results from the futures.
    for i, future in tqdm.tqdm_gui(enumerate(futures)):
        try:
            out.append(future.result())
        except Exception as e:
            out.append(e)

    return front + out


def get_big_number(i, how_many):
    '''
    only for tests. Generates a big number
    :param i: factor
    :param how_many: iterations of additions
    '''
    return sum([100000 * 100000 * i for i in range(how_many)])


if __name__ == '__main__':
    '''
    build an array (arr) of dicts. Each dict has all parameters (i, how_many)
    of function (get_big_number) for parallel processing. in this example
    1000 processes are started
    '''
    arr = [{'i': i, 'how_many': 100000 if i % 2 else 220000}
           for i in range(10)]
    # show 1st 10 dicts
    for i in range(10):
        print (i, " ", arr[i])

    list_of_big = parallel_process_gui(
        arr,
        get_big_number,
        desc="progress 1",
        front_num=0,
        use_kwargs=True)

    arr = [{'i': i, 'how_many': 100000 if i % 2 else 220000}
           for i in range(1000)]

    #run it again, now there is one (old) window in background of 
    #a new progressbar 
    list_of_big = parallel_process_gui(
        arr,
        get_big_number,
        desc="progress 2",
        front_num=0,
        use_kwargs=True)

    # show 1st 10 results
    for i in range(10):
        print (i, " ", list_of_big[i])

    # show last 10 results
    for i in range(990, 1000):
        print (i, " ", list_of_big[i])

    sleep(10)

我知道 tqdm_gui 仍处于实验性/alpha 阶段,但我希望在完成解析工作后关闭进度条。

任何帮助将不胜感激。

托马斯

【问题讨论】:

    标签: python-3.x user-interface tqdm


    【解决方案1】:

    我发现了我的错误。

    在最后的parallel_process_gui中我改变了获取结果

        # Get the results from the futures.
        for i, future in tqdm.tqdm_gui(enumerate(futures)):
            try:
                out.append(future.result())
            except Exception as e:
                out.append(e)
    

        # Get the results from the futures.
        for i, future in enumerate(futures):
            try:
                out.append(future.result())
            except Exception as e:
                out.append(e)
    

    这行得通!使用

    迭代结果
    tqdm.tqdm_gui(enumerate(futures))
    

    打开不需要的图形。

    托马斯

    【讨论】:

      猜你喜欢
      • 2023-01-08
      • 2018-07-17
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多