0

I have a function written in Python. I want to get the average (or peak) cpu and memory usage while the function is running.

However, I do not want to do those measurements from inside the function - I do not want to change the function itself.

I want to call the function and make the measurements while the function is running.

How should I address this?

I have looked into psutil, but still not sure how to do it.

Thank you very much!

Dan
  • 25
  • 3
  • you could create function which get values from `psutil`, runs your original function, and get again `psutil`. And they you can repalce functions. You could even create `decorator` for this. As I know there was module which use decorator to run `time` before and after executing function. See [timeit versus timing decorator](https://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator) to see how to create decoreator for measurements. – furas Apr 27 '21 at 08:42
  • if you want to measure it all time while function is running then you may have to run `psutil` in separated thread - and run loop which get values every few (milli)seconds. – furas Apr 27 '21 at 08:44

0 Answers0