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.