cURL 函数
在线手册:中文 英文
PHP手册

curl_multi_select

(PHP 5)

curl_multi_select等待所有cURL批处理中的活动连接

说明

int curl_multi_select ( resource $mh [, float $timeout = 1.0 ] )

阻塞直到cURL批处理连接中有活动连接。

参数

mh

curl_multi_init() 返回的 cURL 多个句柄。

timeout

以秒为单位,等待响应的时间。

返回值

成功时返回描述符集合中描述符的数量。失败时,select失败时返回-1,否则返回超时(从底层的select系统调用).

参见


cURL 函数
在线手册:中文 英文
PHP手册
PHP手册 - N: 等待所有cURL批处理中的活动连接

用户评论:

Anonymous (27-Aug-2011 01:52)

if you are using a non zts php build, expect it to block on connect() calls when using curl multi, if you plan to use curl multi to realtime fetch multiple pages for a user, have a localhost squid setup to funnel them through so as connect() only blocks for a ms or 2

Anonymous (24-Jul-2010 04:33)

Since the docs are still lacking, here's an example of how to use the function. The following code will keep checking all active threads until one of them returns the HTTP 200 Ok status code, or simply end. On success, it will return the URL that worked.

<?php
$running
=null;
do {
   
curl_multi_exec($mh,$running);
   
$ready=curl_multi_select($mh); // this will pause the loop
   
if($ready>0){
        while(
$info=curl_multi_info_read($mh)){
           
$status=curl_getinfo($info['handle'],CURLINFO_HTTP_CODE);
            if(
$status==200){
               
$successUrl=curl_getinfo($info['handle'],CURLINFO_EFFECTIVE_URL);
                break
2;
            }
        }
    }
} while (
$running>0 && $ready!=-1);
?>

The question for the $ready variable is, if it will return the value before or after the timeout has occurred. From my tests it appears that it will return the value immediately, and only then pause the execution. That's because it's always zero the first time through the loop, even if the time limit is as high as 10sec. I've expected it to wait and only then return the value so that came unexpected to me.

As stated by someone else, it also doesn't seem to return the overall count of threads in the handle, but only that of the currently active ones.

public at grik dot net (10-Aug-2008 02:38)

This function blocks the calling process until there is activity on any of the connections opened by the curl_multi interface, or until the timeout period has expired.
In other words, it waits for data to be received in the opened connections.

Internally it fetches socket pointers with "curl_multi_fdset()" and runs the "select()" C function.
It returns in 3 cases:
1. Activity is detected on any socket;
2. Timeout has ended (second parameter);
3. Process received any signal (#man kill).

The function returns an integer:
* In case of activity it returns a number, usually 1.
I suppose, it returns the number of connections with activity detected.
* If timeout expires it returns 0
* In case of error it returns -1

Thanks for attention, hope this helps.