CascadiaPHP 2024


(PECL luasandbox >= 1.4.0)

LuaSandbox::pauseUsageTimerPause the CPU usage timer


public LuaSandbox::pauseUsageTimer(): bool

Pauses the CPU usage timer.

This only has effect when called from within a callback from Lua. When execution returns to Lua, the timer will be automatically unpaused. If a new call into Lua is made, the timer will be unpaused for the duration of that call.

If a PHP callback calls into Lua again with timer not paused, and then that Lua function calls into PHP again, the second PHP call will not be able to pause the timer. The logic is that even though the second PHP call would avoid counting the CPU usage against the limit, the first call still counts it.

Bağımsız Değişkenler

Bu işlevin bağımsız değişkeni yoktur.

Dönen Değerler

Returns a bool indicating whether the timer is now paused.


Örnek 1 Manipulating the usage timer


// create a new LuaSandbox and set a CPU limit
$sandbox = new LuaSandbox();
$sandbox->setCPULimit( 1 );

doWait( $t ) {
$end = microtime( true ) + $t;
while (
microtime( true ) < $end ) {
// waste CPU cycles

// Register a PHP callback
$sandbox->registerLibrary( 'php', [
'test' => function () use ( $sandbox ) {
doWait( 5 );

doWait( 0.1 );
'test2' => function () use ( $sandbox ) {
doWait( 1.1 );
] );

"This should not time out...\n";
$sandbox->loadString( 'php.test()' )->call();

"This should time out.\n";
try {
$sandbox->loadString( 'php.test2()' )->call();
"It did not?\n";
} catch (
LuaSandboxTimeoutError $ex ) {
"It did! " . $ex->getMessage() . "\n";


Yukarıdaki örneğin çıktısı:

This should not time out...
This should time out.
It did! The maximum execution time for this script was exceeded

Ayrıca Bakınız

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top