【问题标题】:Bus error on curl_multi_close() callcurl_multi_close() 调用上的总线错误
【发布时间】:2014-02-04 14:16:56
【问题描述】:

PHP 脚本用于从 FTP 服务器下载文件。但是当我关闭多处理程序时,php cli 终端显示“总线错误”并终止进程。

哪些行会导致此错误?如何解决?

function download ($host, array $files, $threadsCount = 5) {
    $filesCount = count($fileNames);
    $threadsCount = $filesCount < $threadsCount ? $filesCount : $threadsCount;

    $mh = curl_multi_init();
    $fh = [];

    // Default options for all threads
    $options = [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_MAXREDIRS      => 5,
        CURLOPT_HEADER         => 0,
        CURLOPT_BINARYTRANSFER => 1,
        CURLOPT_TIMEOUT        => 200,
        CURLOPT_CONNECTTIMEOUT => 200,
    ];

    // Init first threads
    for ($i = 0; $i < $threadsCount; $i++) {
        $src = $fileNames[$i];

        // Path to file on server
        $url = $host . '/' . $src;

        // Where to store this file
        $tmp = '/tmp/' . $src;

        $ch = curl_init();
        $fh[$url] = fopen($tmp, 'wb');

        $files[$src] = $tmp;

        curl_setopt_array($ch, $options + [
            CURLOPT_URL  => $url,
            CURLOPT_FILE => $fh[$url]
        ]);

        curl_multi_add_handle($mh, $ch);
    }

    // Process threads
    do {
        while (($state = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM);

        if ($state != CURLM_OK) {
            break;
        }

        // Request is completed
        while ($done = curl_multi_info_read($mh)) {
            // Start a new request before killing old
            if ($i < $filesCount) {
                $src = $fileNames[$i];

                $url = $host . '/' . $src;
                $tmp = '/tmp/' . $src;

                $ch = curl_init();
                $fh[$url] = fopen($tmp, 'wb');

                curl_setopt_array($ch, $options + [
                    CURLOPT_URL  => $url,
                    CURLOPT_FILE => $fh[$url]
                ]);

                curl_multi_add_handle($mh, $ch);
                $i++;
            }

            // Close handlers for files and cURL
            fclose($fh[ curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL) ]);

            curl_multi_remove_handle($mh, $done['handle']);
            curl_close($done['handle']);
        }

        if ($running) {
            curl_multi_select($mh, 10);
        }

    } while ($running);

    // AFTER THIS "BUS ERROR" IS FIRING!
    curl_multi_close($mh);
}

对于某些服务器,此错误不会出现。

【问题讨论】:

    标签: php curl ftp download libcurl


    【解决方案1】:

    这听起来像是 libcurl 库或 PHP/CURL 绑定中的错误。如果您没有使用最新版本,那么您有理由升级。

    如果您确实使用了最新版本,那么您有理由直接处理项目的这个问题。

    【讨论】:

    • 我已经通过将关闭处理程序操作移出所有 while 循环来克服这个问题。似乎为我工作。
    猜你喜欢
    • 2016-10-16
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多