略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: LuaSandbox::setCPULimit

2024-11-14

LuaSandbox::setCPULimit

(PECL luasandbox >= 1.0.0)

LuaSandbox::setCPULimitSet the CPU time limit for the Lua environment

说明

public LuaSandbox::setCPULimit(float|bool $limit): void

Sets the CPU time limit for the Lua environment.

If the total user and system time used by the environment after the call to this method exceeds this limit, a LuaSandboxTimeoutError exception is thrown.

Time used in PHP callbacks is included in the limit.

Setting the time limit from a callback while Lua is running causes the timer to be reset, or started if it was not already running.

注意:

On Windows, the CPU limit will be ignored. On operating systems that do not support CLOCK_THREAD_CPUTIME_ID, such as FreeBSD and Mac OS X, wall-clock time rather than CPU time will be limited.

参数

limit

Limit as a float in seconds, or false for no limit.

返回值

没有返回值。

范例

示例 #1 Calling a Lua function

<?php

// create a new LuaSandbox
$sandbox = new LuaSandbox();

// set a time limit
$sandbox->setCPULimit);

// Run Lua code
$sandbox->loadString'while true do end' )->call();

?>

以上例程的输出类似于:

PHP Fatal error:  Uncaught LuaSandboxTimeoutError: The maximum execution time for this script was exceeded

参见

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/luasandbox.setcpulimit.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3