/usr/share/doc/python-pymongo-doc/html/examples/requests.html is in python-pymongo-doc 2.6.3-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Requests — PyMongo 2.6.3 documentation</title>
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2.6.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="top" title="PyMongo 2.6.3 documentation" href="../index.html" />
<link rel="up" title="Changelog" href="../changelog.html" />
<link rel="next" title="Python 3 FAQ" href="../python3.html" />
<link rel="prev" title="Gevent" href="gevent.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../python3.html" title="Python 3 FAQ"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="gevent.html" title="Gevent"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">PyMongo 2.6.3 documentation</a> »</li>
<li><a href="../changelog.html" accesskey="U">Changelog</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="requests">
<h1>Requests<a class="headerlink" href="#requests" title="Permalink to this headline">¶</a></h1>
<p>PyMongo supports the idea of a <em>request</em>: a series of operations executed with
a single socket, which are guaranteed to be processed on the server in the same
order as they ran on the client.</p>
<p>Requests are not usually necessary with PyMongo.
By default, the methods <a class="reference internal" href="../api/pymongo/collection.html#pymongo.collection.Collection.insert" title="pymongo.collection.Collection.insert"><tt class="xref py py-meth docutils literal"><span class="pre">insert()</span></tt></a>,
<a class="reference internal" href="../api/pymongo/collection.html#pymongo.collection.Collection.update" title="pymongo.collection.Collection.update"><tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt></a>,
<a class="reference internal" href="../api/pymongo/collection.html#pymongo.collection.Collection.save" title="pymongo.collection.Collection.save"><tt class="xref py py-meth docutils literal"><span class="pre">save()</span></tt></a>, and
<a class="reference internal" href="../api/pymongo/collection.html#pymongo.collection.Collection.remove" title="pymongo.collection.Collection.remove"><tt class="xref py py-meth docutils literal"><span class="pre">remove()</span></tt></a> block until they receive
acknowledgment from the server, so ordered execution is already guaranteed. You
can be certain the next <a class="reference internal" href="../api/pymongo/collection.html#pymongo.collection.Collection.find" title="pymongo.collection.Collection.find"><tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt></a> or
<a class="reference internal" href="../api/pymongo/collection.html#pymongo.collection.Collection.count" title="pymongo.collection.Collection.count"><tt class="xref py py-meth docutils literal"><span class="pre">count()</span></tt></a>, for example, is executed on the
server after the writes complete. This is called “read-your-writes
consistency.”</p>
<p>An example of when a request is necessary is if a series of documents are
inserted with <tt class="docutils literal"><span class="pre">w=0</span></tt> for performance reasons, and you want to query those
documents immediately afterward: With <tt class="docutils literal"><span class="pre">w=0</span></tt> the writes can queue up at the
server and might not be immediately visible in query results. Wrapping the
inserts and queries within
<a class="reference internal" href="../api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient.start_request" title="pymongo.mongo_client.MongoClient.start_request"><tt class="xref py py-meth docutils literal"><span class="pre">start_request()</span></tt></a> and
<a class="reference internal" href="../api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient.end_request" title="pymongo.mongo_client.MongoClient.end_request"><tt class="xref py py-meth docutils literal"><span class="pre">end_request()</span></tt></a> forces a query to be on
the same socket as the inserts, so the query won’t execute until the inserts
are complete on the server side.</p>
<div class="section" id="example">
<h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<p>Let’s consider a collection of web-analytics counters. We want to count the
number of page views our site has served for each combination of browser,
region, and OS, and then show the user the number of page views from his or her
region, <em>including</em> the user’s own visit. We have three ways to do so reliably:</p>
<p>1. Simply update the counters with an acknowledged write (the default), and
then <tt class="docutils literal"><span class="pre">find</span></tt> all counters for the visitor’s region. This will ensure that the
<tt class="docutils literal"><span class="pre">update</span></tt> completes before the <tt class="docutils literal"><span class="pre">find</span></tt> begins, but it comes with a performance
penalty that may be unacceptable for analytics.</p>
<p>2. Create the <a class="reference internal" href="../api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient" title="pymongo.mongo_client.MongoClient"><tt class="xref py py-class docutils literal"><span class="pre">MongoClient</span></tt></a> with <tt class="docutils literal"><span class="pre">w=0</span></tt> and
<tt class="docutils literal"><span class="pre">auto_start_request=True</span></tt> to do unacknowledged writes and ensure each thread
gets its own socket.</p>
<p>3. Explicitly call <a class="reference internal" href="../api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient.start_request" title="pymongo.mongo_client.MongoClient.start_request"><tt class="xref py py-meth docutils literal"><span class="pre">start_request()</span></tt></a>,
then do the unacknowledged updates and the queries within the request. This
third method looks like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">()</span>
<span class="gp">>>> </span><span class="n">counts</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">requests_example</span><span class="o">.</span><span class="n">counts</span>
<span class="gp">>>> </span><span class="n">region</span><span class="p">,</span> <span class="n">browser</span><span class="p">,</span> <span class="n">os</span> <span class="o">=</span> <span class="s">'US'</span><span class="p">,</span> <span class="s">'Firefox'</span><span class="p">,</span> <span class="s">'Mac OS X'</span>
<span class="gp">>>> </span><span class="n">request</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">start_request</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">counts</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'region'</span><span class="p">:</span> <span class="n">region</span><span class="p">,</span> <span class="s">'browser'</span><span class="p">:</span> <span class="n">browser</span><span class="p">,</span> <span class="s">'os'</span><span class="p">:</span> <span class="n">os</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s">'n'</span><span class="p">:</span> <span class="mi">1</span> <span class="p">}},</span>
<span class="gp">... </span> <span class="n">upsert</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">w</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="c"># unacknowledged write</span>
<span class="gp">...</span>
<span class="gp">... </span> <span class="c"># This always runs after update has completed:</span>
<span class="gp">... </span> <span class="n">count</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">p</span><span class="p">[</span><span class="s">'n'</span><span class="p">]</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">counts</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s">'region'</span><span class="p">:</span> <span class="n">region</span><span class="p">})])</span>
<span class="gp">... </span><span class="k">finally</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">request</span><span class="o">.</span><span class="n">end</span><span class="p">()</span>
<span class="gp">>>> </span><span class="k">print</span> <span class="n">count</span>
<span class="go">1</span>
</pre></div>
</div>
<p>Requests can also be used as context managers, with the <a class="reference external" href="http://docs.python.org/reference/compound_stmts.html#index-15">with statement</a>, which makes
the previous example more terse:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">client</span><span class="o">.</span><span class="n">in_request</span><span class="p">()</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">client</span><span class="o">.</span><span class="n">start_request</span><span class="p">():</span>
<span class="gp">... </span> <span class="c"># MongoClient is now in request</span>
<span class="gp">... </span> <span class="n">counts</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'region'</span><span class="p">:</span> <span class="n">region</span><span class="p">,</span> <span class="s">'browser'</span><span class="p">:</span> <span class="n">browser</span><span class="p">,</span> <span class="s">'os'</span><span class="p">:</span> <span class="n">os</span><span class="p">},</span>
<span class="gp">... </span> <span class="p">{</span><span class="s">'$inc'</span><span class="p">:</span> <span class="p">{</span><span class="s">'n'</span><span class="p">:</span> <span class="mi">1</span> <span class="p">}},</span>
<span class="gp">... </span> <span class="n">upsert</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">safe</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">print</span> <span class="nb">sum</span><span class="p">([</span><span class="n">p</span><span class="p">[</span><span class="s">'n'</span><span class="p">]</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">counts</span><span class="o">.</span><span class="n">find</span><span class="p">({</span><span class="s">'region'</span><span class="p">:</span> <span class="n">region</span><span class="p">})])</span>
<span class="go">2</span>
<span class="gp">>>> </span><span class="n">client</span><span class="o">.</span><span class="n">in_request</span><span class="p">()</span> <span class="c"># request automatically ended</span>
<span class="go">False</span>
</pre></div>
</div>
</div>
<div class="section" id="requests-and-max-pool-size">
<h2>Requests And <tt class="docutils literal"><span class="pre">max_pool_size</span></tt><a class="headerlink" href="#requests-and-max-pool-size" title="Permalink to this headline">¶</a></h2>
<p>A thread in a request retains exclusive access to a socket until its request
ends or the thread dies; thus, applications in which more than 100 threads are
in requests at once should disable the <tt class="docutils literal"><span class="pre">max_pool_size</span></tt> option:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">client</span> <span class="o">=</span> <span class="n">MongoClient</span><span class="p">(</span><span class="n">host</span><span class="p">,</span> <span class="n">port</span><span class="p">,</span> <span class="n">max_pool_size</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>Failure to increase or disable <tt class="docutils literal"><span class="pre">max_pool_size</span></tt> in such an application can
leave threads forever waiting for sockets.</p>
<p>See <a class="reference internal" href="../faq.html#connection-pooling"><em>How does connection pooling work in PyMongo?</em></a></p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Requests</a><ul>
<li><a class="reference internal" href="#example">Example</a></li>
<li><a class="reference internal" href="#requests-and-max-pool-size">Requests And <tt class="docutils literal"><span class="pre">max_pool_size</span></tt></a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="gevent.html"
title="previous chapter">Gevent</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="../python3.html"
title="next chapter">Python 3 FAQ</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/examples/requests.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="../python3.html" title="Python 3 FAQ"
>next</a> |</li>
<li class="right" >
<a href="gevent.html" title="Gevent"
>previous</a> |</li>
<li><a href="../index.html">PyMongo 2.6.3 documentation</a> »</li>
<li><a href="../changelog.html" >Changelog</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2008 - 2012, 10gen, Inc..
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.
</div>
</body>
</html>
|