<!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>Concurrency, locking, and versioning — Whoosh 2.5.7 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.5.7', 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> <link rel="top" title="Whoosh 2.5.7 documentation" href="index.html" /> <link rel="next" title="Indexing and searching document hierarchies" href="nested.html" /> <link rel="prev" title="Tips for speeding up batch indexing" href="batch.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="nested.html" title="Indexing and searching document hierarchies" accesskey="N">next</a> |</li> <li class="right" > <a href="batch.html" title="Tips for speeding up batch indexing" accesskey="P">previous</a> |</li> <li><a href="index.html">Whoosh 2.5.7 documentation</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="concurrency-locking-and-versioning"> <h1>Concurrency, locking, and versioning<a class="headerlink" href="#concurrency-locking-and-versioning" title="Permalink to this headline">¶</a></h1> <div class="section" id="concurrency"> <h2>Concurrency<a class="headerlink" href="#concurrency" title="Permalink to this headline">¶</a></h2> <p>The <tt class="docutils literal"><span class="pre">FileIndex</span></tt> object is “stateless” and should be share-able between threads.</p> <p>A <tt class="docutils literal"><span class="pre">Reader</span></tt> object (which underlies the <tt class="docutils literal"><span class="pre">Searcher</span></tt> object) wraps open files and often individual methods rely on consistent file cursor positions (e.g. they do two <tt class="docutils literal"><span class="pre">file.read()</span></tt>s in a row, so if another thread moves the cursor between the two read calls Bad Things would happen). You should use one Reader/Searcher per thread in your code.</p> <p>Readers/Searchers tend to cache information (such as field caches for sorting), so if you can share one across multiple search requests, it’s a big performance win.</p> </div> <div class="section" id="locking"> <h2>Locking<a class="headerlink" href="#locking" title="Permalink to this headline">¶</a></h2> <p>Only one thread/process can write to an index at a time. When you open a writer, it locks the index. If you try to open a writer on the same index in another thread/process, it will raise <tt class="docutils literal"><span class="pre">whoosh.store.LockError</span></tt>.</p> <p>In a multi-threaded or multi-process environment your code needs to be aware that opening a writer may raise this exception if a writer is already open. Whoosh includes a couple of example implementations (<a class="reference internal" href="api/writing.html#whoosh.writing.AsyncWriter" title="whoosh.writing.AsyncWriter"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.writing.AsyncWriter</span></tt></a> and <a class="reference internal" href="api/writing.html#whoosh.writing.BufferedWriter" title="whoosh.writing.BufferedWriter"><tt class="xref py py-class docutils literal"><span class="pre">whoosh.writing.BufferedWriter</span></tt></a>) of ways to work around the write lock.</p> <p>While the writer is open and during the commit, <strong>the index is still available for reading</strong>. Existing readers are unaffected and new readers can open the current index normally.</p> <div class="section" id="lock-files"> <h3>Lock files<a class="headerlink" href="#lock-files" title="Permalink to this headline">¶</a></h3> <p>Locking the index is accomplished by acquiring an exclusive file lock on the <tt class="docutils literal"><span class="pre"><indexname>_WRITELOCK</span></tt> file in the index directory. The file is not deleted after the file lock is released, so the fact that the file exists <strong>does not</strong> mean the index is locked.</p> </div> </div> <div class="section" id="versioning"> <h2>Versioning<a class="headerlink" href="#versioning" title="Permalink to this headline">¶</a></h2> <p>When you open a reader/searcher, the reader represents a view of the <strong>current version</strong> of the index. If someone writes changes to the index, any readers that are already open <strong>will not</strong> pick up the changes automatically. A reader always sees the index as it existed when the reader was opened.</p> <p>If you are re-using a Searcher across multiple search requests, you can check whether the Searcher is a view of the latest version of the index using <a class="reference internal" href="api/searching.html#whoosh.searching.Searcher.up_to_date" title="whoosh.searching.Searcher.up_to_date"><tt class="xref py py-meth docutils literal"><span class="pre">whoosh.searching.Searcher.up_to_date()</span></tt></a>. If the searcher is not up to date, you can get an up-to-date copy of the searcher using <a class="reference internal" href="api/searching.html#whoosh.searching.Searcher.refresh" title="whoosh.searching.Searcher.refresh"><tt class="xref py py-meth docutils literal"><span class="pre">whoosh.searching.Searcher.refresh()</span></tt></a>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># If 'searcher' is not up-to-date, replace it</span> <span class="n">searcher</span> <span class="o">=</span> <span class="n">searcher</span><span class="o">.</span><span class="n">refresh</span><span class="p">()</span> </pre></div> </div> <p>(If the searcher has the latest version of the index, <tt class="docutils literal"><span class="pre">refresh()</span></tt> simply returns it.)</p> <p>Calling <tt class="docutils literal"><span class="pre">Searcher.refresh()</span></tt> is more efficient that closing the searcher and opening a new one, since it will re-use any underlying readers and caches that haven’t changed.</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="#">Concurrency, locking, and versioning</a><ul> <li><a class="reference internal" href="#concurrency">Concurrency</a></li> <li><a class="reference internal" href="#locking">Locking</a><ul> <li><a class="reference internal" href="#lock-files">Lock files</a></li> </ul> </li> <li><a class="reference internal" href="#versioning">Versioning</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="batch.html" title="previous chapter">Tips for speeding up batch indexing</a></p> <h4>Next topic</h4> <p class="topless"><a href="nested.html" title="next chapter">Indexing and searching document hierarchies</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/threads.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="nested.html" title="Indexing and searching document hierarchies" >next</a> |</li> <li class="right" > <a href="batch.html" title="Tips for speeding up batch indexing" >previous</a> |</li> <li><a href="index.html">Whoosh 2.5.7 documentation</a> »</li> </ul> </div> <div class="footer"> © Copyright 2007-2012 Matt Chaput. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>