php7防止程序超时的方式,PHP 函数执行超时控制扩展 (For PHP7+) 控制PHP函数的执行时间,...

PHP extension to control function's excution time (For PHP7+)

68747470733a2f2f7472617669732d63692e6f72672f70616e677564617368752f74696d656f75742e7376673f6272616e63683d6d6173746572

It's an extension to control PHP function's excution time.In fact,it just have one function:call_func_with_timeout() which similar to function call_user_func().I add timeout control to it.

Why written this extesion?In my work,I find many circumstances that function executed long time and block the process,so I need a method to control the execution time of function.

When call a function, we can set timeout on it and it will be forced stoped if it's still in executing over the limit time.

This exetension use sigsetjmp() and siglongjmp() to implement the jump of running stack which like PHP's try-catch implement.

Version

v1.0.0

Environment Requirements

OS: linux

Version: PHP 7.0.0 or newer

SAPI: php-fpm or cli

Features

timeout control on function level

support millisecond-level config

Note

current version only support one timmer

Function

int call_func_with_timeout(mixed callable, int interval [, array args [, mixed retval]]

Parames:

$callable array|string [required] //call function name or array(class|object, method)

$interval int [required] //timeout interval,unit:ms

$args array [optional] //function request args

$retval mixed [optional]

Return:[type int]

TT_CALL_FUNCTION_SUCC: //execute success

TT_CALL_FUNCTION_TIMEOUT: //execute timeout

TT_SET_FUNCTION_CALL_INFO_ERROR: //the function called invalid,eg:class or object not exist

TT_FUNCTION_CALL_FAILURE: //execute failed,the same as TT_SET_FUNCTION_CALL_INFO_ERROR,eg:function not exist

TT_SET_SIGNAL_ERROR: //set signal handler error

TT_SET_TIMER_ERROR: //set timer error

Install

git clone git@github.com:pangudashu/timeout.git

cd timeout

phpize

./configure

make && make install

After install, add "extension=timeout.so" to php.ini

Demo

class sync {

public function send($a,$b,$c,$d){

sleep(2);

print_r(get_defined_vars());

return 1024;

}

}

$sync_send_ret = null;

$r = call_func_with_timeout(array("sync", "send"), 1200, array(1,2,3,4), $sync_send_ret);

echo "sync::send() return value:" . $sync_send_ret . "\n";

switch($r)

{

case TT_CALL_FUNCTION_SUCC:

echo 'call function success';

break;

case TT_SET_FUNCTION_CALL_INFO_ERROR:

echo 'set function call info error';

break;

case TT_FUNCTION_CALL_FAILURE:

echo 'call function failure';

break;

case TT_SET_SIGNAL_ERROR:

echo 'set signal handler error';

break;

case TT_SET_TIMER_ERROR:

echo 'set timer error';

break;

case TT_CALL_FUNCTION_TIMEOUT:

echo 'call function timeout';

break;

}

?>

Result output:

sync::send() return value:

call function timeout

If annotate sleep(2),result output:

Array

(

[a] => 1

[b] => 2

[c] => 3

[d] => 4

)

sync::send() return value:1024

call function success

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值