Simple Use Case¶
The usage of the tqdm module is really simple.
All you need is to import the tqdm
function from the tqdm
module,
and then wrap the function tqdm
over the iterable object that you want iterate over.
This is really convenient if you have a long running task
and want to report the progress of the task.
from tqdm import tqdm
arr = [i**2 for i in tqdm(range(100000))]
You can specify the (guessed) total number of iterations using the argument total
.
import time
for i in tqdm(range(100), total=10):
time.sleep(1)
import time
for i in tqdm(range(10), total=100):
time.sleep(1)
Notice that trange(i)
is a special optimised instance of tqdm(range(i))
.
from tqdm import trange
arr = [i**2 for i in trange(0, 100000, 10)]
tqdm
with multiprocessing
¶
The trick is to use multiprocessing.Pool.imap
and use tqdm
outside the map (rather than inside it).
Notice that the time reported might not accurate (especially at the beginning and with long-running tasks).
As more tasks get finished,
the ETA is more reliable.
import multiprocessing as mp
from tqdm import tqdm
with mp.Pool(4) as pool:
for _ in tqdm(pool.imap(task, params), total=total):
pass
Command-line and Pipe¶
tqdm
can also be executed as a module with pipes.
seq 9999999 | tqdm --bytes | wc -l