Forums

CPU seconds usage

I'm thinking of writing some code that does some numerical simulations with a monte carlo approach. Because such an algorithm has an arbitrary run time I would like to be able to limit the amount of time that such a simulation would run. Specifically I would like to be able to do this in terms of the CPU seconds that are in the accounts allowance. Is there an API or some other way to do this?

We don't have a specific API, but maybe you could use psutil? Something like this...

import os
import psutil

this_process = psutil.Process(os.getpid())
print(this_process.cpu_times())