【问题标题】:curl_multi_exec kills whole process when there ia a single timeoutcurl_multi_exec 在有单个超时时杀死整个进程
【发布时间】:2012-09-11 13:50:48
【问题描述】:

我正在使用 curl_multi 在与此类似的滚动 curl 脚本中发送电子邮件,但我添加了 10 秒的 curlopt_timeout 和 20 秒的 curlopt_connecttimeout http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/

在测试它时,我分别使用 timeout_ms 和 connecttimeout_ms 将超时减少到 1ms,只是为了看看它如何处理超时。但是超时会杀死整个 curl 过程。即使有一次超时,有没有办法继续其他线程? 谢谢。

-devo

【问题讨论】:

    标签: php curl


    【解决方案1】:

    https://github.com/krakjoe/pthreads

    <?php
    class Possibilities extends Thread {
        public function __construct($url){
            $this->url = $url;
        }
    
        public function run(){
            /*
            * Or use curl, this is quicker to make an example ...
            */
            return file_get_contents($this->url);
        }
    }
    $threads = array();
    $urls = get_my_urls_from_somewhere();
    foreach($urls as $index => $url){
        $threads[$index]=new Possibilities($url);
        $threads[$index]->start();
    }
    foreach($threads as $index => $thread ){
        if( ( $response = $threads[$index]->join() ) ){
            /** good, got a response */
        } else { /** we do not care **/ }
    }
    ?>
    

    我的猜测是,您正在使用 curl multi,因为它是并发执行发送电子邮件的代码的唯一选项......如果是这种情况,我不建议您使用类似上面代码的任何东西,我建议您直接将调用线程化到 mail(),因为到目前为止这将更快、更有效。

    但现在你知道了,你可以在 PHP 中使用线程 .. 享受 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-08
      • 2012-05-30
      相关资源
      最近更新 更多