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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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:
percent_stepargument defines the granularity level of printing: each time the loop finished processing thepercent_step% amount of data, the total percentage of processed data is printed on the screen.Then can apply this wrapper to the loop:
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
lenwith direct SQL:Another variant that prints progress after the specified amount of seconds: