Advanced

Confusion on Time and API calls

Confusion on Time and API calls
June 12, 2021 02:56PM
Hello,

I recently had a script that was timing out. Another script used an os.date call to get the progression of time. To cache when to timeout.

Looking at the template for a new script, there is a call to PGC.GetCPUUsage() which seems a better choice to get the actually CPU usage as that can occur slower than the passage of time. However this call is not documented on https://project-gc.com/doxygen/lua-sandbox/classPGC__LUA__Sandbox.html

What is the current best practice for getting the passage of time to allow scripts to timeout?

Thanks
Re: Confusion on Time and API calls
April 01, 2024 04:24AM
Ran into this myself just recently... GetCPUUsage appears to return os.clock(), which is the amount of time a CPU has spent running the task. Which really has no relation to the actual amount of time the checker has been running, just the amount of time the checker has had the CPU active. Although apparently on Windows this will be the same as the wallclock time. But, I'll assume PGC isn't running on Windows.

It appears the best way to do this, is call os.time() at the start of your script, and then do os.difftime(os.time(), start_time) periodically to check for the 60 second limit (or near to it). Or at least, that's what I'm doing and it seem to work.
Re: Confusion on Time and API calls
April 02, 2024 01:11PM
"os.difftime(os.time(), start_time)" is probably the best. GetCPUUsage doesn't include time spent on database queries, for instance. The checker will in fact keep running in the background, but the countdown on the checker UI will display a timeout error.
Sorry, you do not have permission to post/reply in this forum.