<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Sets a client-side timeout for this query</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.tailable.html">MongoCursor::tailable</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.valid.html">MongoCursor::valid</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mongocursor.timeout" class="refentry"> <div class="refnamediv"> <h1 class="refname">MongoCursor::timeout</h1> <p class="verinfo">(PECL mongo >=1.0.3)</p><p class="refpurpose"><span class="refname">MongoCursor::timeout</span> — <span class="dc-title">Sets a client-side timeout for this query</span></p> </div> <div class="refsect1 description" id="refsect1-mongocursor.timeout-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.mongocursor.html" class="type MongoCursor">MongoCursor</a></span> <span class="methodname"><strong>MongoCursor::timeout</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$ms</code></span> )</div> <p class="para rdfs-comment"> A timeout can be set at any time and will affect subsequent queries on the cursor, including fetching more results from the database. </p> </div> <div class="refsect1 parameters" id="refsect1-mongocursor.timeout-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"> <em><code class="parameter">ms</code></em> </span> <dd> <p class="para"> The number of milliseconds for the cursor to wait for a response. Use <em>-1</em> to wait forever. By default, the cursor will wait <var class="varname"><var class="varname">MongoCursor::$timeout</var></var> milliseconds. </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-mongocursor.timeout-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> This cursor. </p> </div> <div class="refsect1 errors" id="refsect1-mongocursor.timeout-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> Causes methods that fetch results to throw a <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> if the query takes longer than the specified number of milliseconds. </p> </div> <div class="refsect1 examples" id="refsect1-mongocursor.timeout-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1488"> <p><strong>Example #1 <span class="function"><strong>MongoCursor::timeout()</strong></span> example</strong></p> <div class="example-contents"><p> In the following example, the driver will wait forever for the initial database response, and then wait 100ms for subsequent responses. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$cursor </span><span style="color: #007700">= </span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">find</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">timeout</span><span style="color: #007700">(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* $cursor->hasNext() executes the query. An infinite timeout has been set, so<br /> * the driver will wait as long as necessary for a response.<br /> */<br /></span><span style="color: #007700">while (</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">hasNext</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">timeout</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* A timeout has now been set, so if the cursor needs to get more results<br /> * from the database, it will only wait 100ms for a response.<br /> */<br /> </span><span style="color: #007700">try {<br /> </span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$cursor</span><span style="color: #007700">-></span><span style="color: #0000BB">getNext</span><span style="color: #007700">());<br /> } catch (</span><span style="color: #0000BB">MongoCursorTimeoutException $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"query took too long!"</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="refsect1 notes" id="refsect1-mongocursor.timeout-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> 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 <a href="class.mongocursortimeoutexception.html" class="classname">MongoCursorTimeoutException</a> after a set time. </p> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mongocursor.tailable.html">MongoCursor::tailable</a></div> <div class="next" style="text-align: right; float: right;"><a href="mongocursor.valid.html">MongoCursor::valid</a></div> <div class="up"><a href="class.mongocursor.html">MongoCursor</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>