LuaSandbox::pauseUsageTimer

(PECL luasandbox >= 1.4.0)

LuaSandbox::pauseUsageTimerPause the CPU usage timer

Description

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.

Parameters

This function has no parameters.

Return Values

Returns a bool indicating whether the timer is now paused.

Examples

Example #1 Manipulating the usage timer

<?php

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

function 
doWait$t ) {
    
$end microtimetrue ) + $t;
    while ( 
microtimetrue ) < $end ) {
        
// waste CPU cycles
    
}
}

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

        
$sandbox->unpauseUsageTimer();
        
doWait0.1 );
    },
    
'test2' => function () use ( $sandbox ) {
        
$sandbox->pauseUsageTimer();
        
$sandbox->unpauseUsageTimer();
        
doWait1.1 );
    }
] );

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

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

?>

The above example will output:

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

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial