When I run my Python script, there is some function that takes up to a few minutes to complete, so I want to display on the shell some kind of timer that informs the user on the time elapsed.
Is there any such thing already ready to use in Python?
When I run my Python script, there is some function that takes up to a few minutes to complete, so I want to display on the shell some kind of timer that informs the user on the time elapsed.
Is there any such thing already ready to use in Python?
One simplistic way is to include a clock in your
sys.ps1
prompt (the thing that normally defines the>>>
prompt)From the documentation for
sys.ps1
:In
~/.local/usercustomize.py
(or more accurately, in whatever folderpython -c 'import site; print site.USER_BASE'
displays), you can add:Then your prompt will look like this:
It's not perfect, as the last time will be when the prompt appeared, not when the line was executed, but it might be of help. You could easily make this display the time in seconds between
__repr__
invokations, and show that in the prompt.The simplest way to do it would be to calculate the elapsed time in the function that takes a few minutes to complete and simply print that time to the shell. However depending on your function this probably is not the best solution.
The second way to do it would to use multi-threading. So have the function that takes awhile run in a thread, while your program then sits in a loop and prints out the elapsed time every so often and looks for the thread to be completed.
Something like:
You can then get fancier by overwriting the time in the shell or even better, using a gui to display a progress bar!
If you are on a Linux or BSD system, try the
pv
command (http://www.ivarch.com/programs/pv.shtml).It will give you a timer and depending on how you code the output of your app, some other stats as well.
Are you talking about measuring the time it takes a function to complete and then print the HH:MM:SS.MS?
You can do:
you can use
datetime
for example,