美文网首页
python|tqdm基础使用

python|tqdm基础使用

作者: reallocing | 来源:发表于2019-11-20 14:31 被阅读0次
1.对While循环使用tqdm

You can use manual control in tqdm by specifying a total argument in the constructor. Verbatim from the manual:

pbar = tqdm(total=100)
for i in range(10):
    pbar.update(10)
pbar.close()

For this to work you need to know the total number of expected runs. In your code it could look something like

pbar = tqdm(total = runs+1)
while currentData[0] <= runs:

    ### ROLLING THE DICES PROCESS ###
    dices = twinDiceRoll()
    currentData[1] += dices[2]  # Updating the current tile

    ### SURPASSING THE NUMBER OF TILES ONBOARD ###
    if currentData[1] > 37:   # If more than a table turn is achieved,
        currentData[0] += 1   # One more turn is registered
        currentData[1] -= 38  # Update the tile to one coresponding to a board tile.
        pbar.update(1)
    else:
        pass
pbar.close()

However, this code isn't perfect: consider if the currentData[1] is always less than 37 -- the progress bar will just stop and not update. If you try to update it in the else:... part, you might violate the total upper bound. This is a start tho :)

2.对进程使用tqdm

You can wrap tqdm around the executor as the following to track the progress:

list(tqdm(executor.map(f, iter), total=len(iter))

Here is your example:

import time  
import concurrent.futures
from tqdm import tqdm

def f(x):
    time.sleep(0.001)  # to visualize the progress
    return x**2

def run(f, my_iter):
    with concurrent.futures.ThreadPoolExecutor() as executor:
        results = list(tqdm(executor.map(f, my_iter), total=len(my_iter)))
    return results

my_iter = range(100000)
run(f, my_iter)

And the result is like this:

16%|██▏           | 15707/100000 [00:00<00:02, 31312.54it/s]
# You can use this function:

from tqdm import tqdm
import concurrent.futures
def tqdm_parallel_map(executor, fn, *iterables, **kwargs):
    """
    Equivalent to executor.map(fn, *iterables),
    but displays a tqdm-based progress bar.
    
    Does not support timeout or chunksize as executor.submit is used internally
    
    **kwargs is passed to tqdm.
    """
    futures_list = []
    for iterable in iterables:
        futures_list += [executor.submit(fn, i) for i in iterable]
    for f in tqdm(concurrent.futures.as_completed(futures_list), total=len(futures_list), **kwargs):
        yield f.result()

Note that internally, executor.submit() is used, not executor.map() because there is no way of calling concurrent.futures.as_completed() on the iterator returned by executor.map().

Note: In constract to executor.map() this function does NOT yield the arguments in the same order as the input.

相关文章

  • python|tqdm基础使用

    1.对While循环使用tqdm You can use manual control in tqdm by sp...

  • python进度条之tqdm模块

    使用tqdm.tqdm()做进度条 import time import tqdm # tqdm()是这个进度条模...

  • tqdm

    Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息用法:tqdm(itera...

  • python tqdm进度条

    Python 使用tqdm可以展示进度条,具体用法: 可以看到trange里面有选项ascii=True,这是为了...

  • tqdm && progressbar的使用

    tqdm 效果如下: 只要是可迭代的对象,均可使用使用tqdm progressbar 输出: 其中widgets...

  • 标准库--tqdm模块--进度条库

    tqdm模块是Python的进度条库,主要分为两种运行模式 1. 基于迭代对象运行:tqdm(iterator) ...

  • tqdm使用

    github地址:https://github.com/noamraph/tqdm 看《流畅的python》时,作...

  • Python tqdm & enumerate

    1.tqdm 用来显示进度条 retry用来实现重试,(1)用retry之前:import timedef do...

  • 针对tqdm和print的顺序问题

    最近使用python的tqdm包的时候,当结合print语句的时候,发现了一些问题 代码为: 结果为: print...

  • Python中tqdm模块的用法

    一、简介 tqdm是Python中专门用于进度条美化的模块,通过在非while的循环体内嵌入tqdm,可以得到一个...

网友评论

      本文标题:python|tqdm基础使用

      本文链接:https://www.haomeiwen.com/subject/dvsoictx.html