超时处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zchqq/article/details/82893920

fopen超时

$timeout=[
 'http'=>[
     'timeout'=>5 //设置一个超时时间,单位为秒
  ]
];

$ctx=stream_context_create($timeout);

if($fp=fopen(“http://example.com/”,”r”,false,$ctx)){
    while($c=fread($fp,8192)){
    echo $c;
}

fclose($fp);

file_get_contents超时

$timeout = [
   'http'=>[
       'timeout'=>5 //设置一个超时时间,单位为秒
    ]
];

$ctx=stream_context_create($timeout);

$text=file_get_contents(“http://example.com/”,0,$ctx);

curl请求超时

CURL 是我们常用的一种比较靠谱的访问HTTP协议接口的lib库,性能高,还有一些并发支持的功能等。
curl_setopt($ch,opt)可以设置一些超时的设置,主要包括:
*(重要)CURLOPT_TIMEOUT  设置cURL允许执行的最长秒数。
*(重要)CURLOPT_TIMEOUT_MS 设置cURL允许执行的最长毫秒数
CURLOPT_CONNECTTIMEOUT 在发起连接前等待的时间,如果设置为0,则无限等待。
CURLOPT_CONNECTTIMEOUT_MS 尝试连接等待的时间,以毫秒为单位。如果设置为0,则无限等待。
CURLOPT_DNS_CACHE_TIMEOUT 设置在内存中保存DNS信息的时间,默认为120秒。

curl普通秒级超时:
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,60);//只需要设置一个秒的数量就可以
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_USERAGENT,$defined_vars['HTTP_USER_AGENT']);

curl普通秒级超时使用:
curl_setopt($ch,CURLOPT_TIMEOUT,60);

curl如果需要进行毫秒超时,需要增加:
curl_easy_setopt(curl,CURLOPT_NOSIGNAL,1L);
或者是:
curl_setopt($ch,CURLOPT_NOSIGNAL,true);是可以支持毫秒级别超时设置的

curl一个毫秒级超时的例子:
<?php
if(!isset($_GET['foo'])){
    $ch=curl_init(‘http://example.com/’);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_NOSIGNAL,1); //注意,毫秒超时一定要设置这个
    curl_setopt($ch,CURLOPT_TIMEOUT_MS,200); //超时毫秒
    $data=curl_exec($ch);
    $curl_errno=curl_errno($ch);
    $curl_error=curl_error($ch);
    curl_close($ch);

    if($curl_errno>0){
       echo”cURLError($curl_errno):$curl_errorn”;
    }else{
       echo”Datareceived:$datan”;
    }

}else{
    sleep(10);
    echo”Done.”;
}

php页面访问

配置:php.ini
max_execution_time=30

或者在代码里设置:
ini_set(“max_execution_time”,30); 
set_time_limit(30);

php-fpm

配置:php-fpm.conf
<?xmlversion=”1.0″?>
<configuration>
Setsthelimitonthenumberofsimultaneousrequeststhatwillbeserved.
EquivalenttoApacheMaxClientsdirective.
EquivalenttoPHP_FCGI_CHILDRENenvironmentinoriginalphp.fcgi
Usedwithanypm_style.
#php-cgi的进程数量
<valuename=”max_children”>128</value>
Thetimeout(inseconds)forservingasinglerequestafterwhichtheworkerprocesswillbeterminated
Shouldbeusedwhen’max_execution_time’inioptiondoesnotstopscriptexecutionforsomereason
’0s’means’off’

#php-fpm 请求执行超时时间,0s为永不超时,否则设置一个 Ns 为超时的秒数
<valuename=”request_terminate_timeout”>0s</value>
Thetimeout(inseconds)forservingofsinglerequestafterwhichaphpbacktracewillbedumpedtoslow.logfile
’0s’means’off’
<valuename=”request_slowlog_timeout”>0s</value>
</configuration>

 nginx

配置:nginx.conf
http{

   #Fastcgi:(针对后端的fastcgi生效,fastcgi不属于proxy模式)
   fastcgi_connect_timeout5;#连接超时
   fastcgi_send_timeout10; #写超时
   fastcgi_read_timeout10;#读取超时

   #Proxy:(针对proxy/upstreams的生效)
   proxy_connect_timeout15s;#连接超时
   proxy_read_timeout24s;#读超时
   proxy_send_timeout10s; #写超时

}

 mysql

        if(!defined('MYSQL_OPT_READ_TIMEOUT')){
           define('MYSQL_OPT_READ_TIMEOUT',11);
        }

        if(!defined('MYSQL_OPT_WRITE_TIMEOUT')){
           define('MYSQL_OPT_WRITE_TIMEOUT',12);
        }
        //设置超时
        $mysqli=mysqli_init();
        $mysqli->options(MYSQL_OPT_READ_TIMEOUT,3);
        $mysqli->options(MYSQL_OPT_WRITE_TIMEOUT,1);

        //连接数据库
        $mysqli->real_connect("localhost","root","root","test");
        if(mysqli_connect_errno()){
            printf("Connectfailed:%s/n",mysqli_connect_error());
            exit();
        }

        //执行查询sleep1秒不超时
        printf("Hostinformation:%s/n",$mysqli->host_info);
        if(!($res=$mysqli->query('selectsleep(1)'))){
           echo "query1error:" . $mysqli->error . "/n";
        }else{
           echo "Query1:querysuccess/n";
        }

        //执行查询sleep9秒会超时
        if(!($res=$mysqli->query('selectsleep(9)'))){
           echo "query2error:" . $mysqli->error . "/n";
        }else{
           echo"Query2:querysuccess/n";
        }

        $mysqli->close();
        echo "closemysqlconnection/n";

猜你喜欢

转载自blog.csdn.net/zchqq/article/details/82893920