Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8342081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:40:07+00:00 2026-06-09T05:40:07+00:00

Sometimes, while conducting long-running measurements in some loop in python, I have no clue

  • 0

Sometimes, while conducting long-running measurements in some loop in python, I have no clue when my measurements are going to finish.

So I needed a tool to print some progress along the way. See my answer below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:40:08+00:00Added an answer on June 9, 2026 at 5:40 am

    Most of the time such measurement involves some heavy processing inside the main loop, so I developed a simple wrapper function that prints loop progress for any iterable:

    from datetime import datetime
    def print_progress(iterable, percent_step=1):
        total = float(len(iterable))
        # or 1 means that iterable has < 100 elems
        abs_step = int((total * percent_step)/100) or 1
        for i, obj in enumerate(iterable):
            if i and not i % abs_step:
                 print "{0:.2%} processed, {1:%H:%M:%S}".format(i/total, datetime.now())
            yield obj
    

    percent_step argument defines the granularity level of printing: each time the loop finished processing the percent_step % amount of data, the total percentage of processed data is printed on the screen.

    Then can apply this wrapper to the loop:

    for x in print_progress(my_list):
        # processing
        ...
    

    The same iterator could be applied for django querysets, but calculating total object count before the loop could be expensive by itself, so in case of retrieving all objects from the database like PostgreSQL it is better to replace len with direct SQL:

    "select reltuples from pg_class where relname='%s'" % table_name
    

    Another variant that prints progress after the specified amount of seconds:

    def print_progress(iterable, second_step=10):
        total = float(len(iterable))
        import time
        time1 = time.time()
        for i, obj in enumerate(iterable):
            if time.time() - time1 > second_step:
                 print "{0:.2%} processed".format(i/total)
                 time1 = time.time()
            yield obj
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While working with ssh I sometimes write to long string/commands eg. command -param 3
I am sure a lot of you have experienced this. Sometimes while debugging a
I am getting this exception sometimes while running my Windows Forms app for a
While looking at online code samples, I have sometimes come across an assignment of
While editing and fixing up code I have thought that sometimes it would be
While I am aware of the duck-typing concept of Python, I sometimes struggle with
I have a slow method, while executing it on timer (System.Timers.Timer). It sometimes gets
I have been using MVP for a while .Sometimes I get confused what actions
Sometimes while writing Java in Eclipse, I write code that generates warnings. A common
Sometimes when I open my app (either while resuming it after it was dormant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.