文字

MongoCursor::timeout

(PECL mongo >=1.0.3)

MongoCursor::timeoutSets a client-side timeout for this query

说明

public MongoCursor MongoCursor::timeout ( int $ms )

A timeout can be set at any time and will affect subsequent queries on the cursor, including fetching more results from the database.

参数

ms

The number of milliseconds for the cursor to wait for a response. Use -1 to wait forever. By default, the cursor will wait 30000 milliseconds (30 seconds).

返回值

This cursor.

错误/异常

Causes methods that fetch results to throw a MongoCursorTimeoutException if the query takes longer than the specified number of milliseconds.

范例

Example #1 MongoCursor::timeout() example

In the following example, the driver will wait forever for the initial database response, and then wait 100ms for subsequent responses.

<?php

$cursor 
$collection -> find ();
$cursor -> timeout (- 1 );


while ( $cursor -> hasNext ()) {
    
$cursor -> timeout ( 100 );

    

    
try {
        
print_r ( $cursor -> getNext ());
    } catch (
MongoCursorTimeoutException $e ) {
        echo 
"query took too long!" ;
    }
}

?>

注释

Warning

This does not cause the MongoDB server to cancel long-running operations; it only instructs the driver to stop waiting for a response and throw a MongoCursorTimeoutException after a set time. If you need to specify a server-side timeout for a query, consider using MongoCursor::maxTimeMS() .

参见

  • MongoCursorInterface::timeout()
  • The socketTimeoutMS option for MongoClient::__construct()

用户评论:

[#1] Robert Ross [2011-04-21 11:10:43]

To set timeouts for all queries, do this:

MongoCursor::$timeout = -1 (-1 is no timeout)

上一篇: 下一篇: