tqdm package

A useful package tells you the progress

Posted by CodingMrWang on August 3, 2018

This post is first created by CodingMrWang, 作者 @Zexian Wang ,please keep the original link if you want to repost it.

  1. tqdm

    tqdm is a fast and extensible progress bar. It is really useful when you need run a long loop, tqdm could predict how much time it will take.

    Install:

     pip install tqdm
    

    How to use:

    from tqdm import tqdm
    for i in tqdm(range(10000)): 
        some_function()
            
    

    sample

     for i in trange(100):
         some_function()
    

    Also, you can update tqdm manually:

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

    Here is a part of code I used tqdm in my program.

    import Queue
    import threading
    from tqdm import tqdm
       
    from web.other.models import Student
       
    class Sample(object):
        def __init__(self):
            self.queue = Queue.Queue()
         	 self.init()
            	 
        def init(self):
            all_data = Student.objects.filter(level=2).all()
            for d in all_data:
                self.queue.put(d)
            pbar = tqdm(total = queue.qsize())
          
       def get_relationship(self):
           while self.queue.qsize():
               try:
                   d = self.queue.get(0)
                   pbar.update(1)
               except:
                   pbar.close()
                   return None
               some_function(d)
         
      def run(self):
          threads = [Thread.threading(target=get_relationship) for _ in range(10)]
          [u.start() for u in threads]
          [u.join() for u in threads]
        
           
    

    This package gave me a big hand when I crawl a huge amount of data, I can see how long will it take and inform PM.