/usr/share/doc/python3-postgresql/html/_modules/postgresql/alock.html is in python3-postgresql 1.1.0-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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | <!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>postgresql.alock — py-postgresql 1.1.0 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: '1.1.0',
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="py-postgresql 1.1.0 documentation" href="../../index.html" />
<link rel="up" title="postgresql" href="../postgresql.html" />
<link rel="stylesheet" href="_static/unsuck.css" type="text/css" />
</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><a href="../../index.html">py-postgresql 1.1.0 documentation</a> »</li>
<li><a href="../index.html" >Module code</a> »</li>
<li><a href="../postgresql.html" accesskey="U">postgresql</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<h1>Source code for postgresql.alock</h1><div class="highlight"><pre>
<span class="c">##</span>
<span class="c"># .alock - Advisory Locks</span>
<span class="c">##</span>
<span class="sd">"""</span>
<span class="sd">Tools for Advisory Locks</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">abstractmethod</span><span class="p">,</span> <span class="n">abstractproperty</span>
<span class="kn">from</span> <span class="nn">.python.element</span> <span class="kn">import</span> <span class="n">Element</span>
<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
<span class="s">'ALock'</span><span class="p">,</span>
<span class="s">'ExclusiveLock'</span><span class="p">,</span>
<span class="s">'ShareLock'</span><span class="p">,</span>
<span class="p">]</span>
<div class="viewcode-block" id="ALock"><a class="viewcode-back" href="../../reference.html#postgresql.alock.ALock">[docs]</a><span class="k">class</span> <span class="nc">ALock</span><span class="p">(</span><span class="n">Element</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Advisory Lock class for managing the acquisition and release of a sequence</span>
<span class="sd"> of PostgreSQL advisory locks.</span>
<span class="sd"> ALock()'s are fairly consistent with threading.RLock()'s. They can be</span>
<span class="sd"> acquired multiple times, and they must be released the same number of times</span>
<span class="sd"> for the lock to actually be released.</span>
<span class="sd"> A notably difference is that ALock's manage a sequence of lock identifiers.</span>
<span class="sd"> This means that a given ALock() may represent multiple advisory locks.</span>
<span class="sd"> """</span>
<span class="n">_e_factors</span> <span class="o">=</span> <span class="p">(</span><span class="s">'database'</span><span class="p">,</span> <span class="s">'identifiers'</span><span class="p">,)</span>
<span class="n">_e_label</span> <span class="o">=</span> <span class="s">'ALOCK'</span>
<span class="k">def</span> <span class="nf">_e_metas</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
<span class="n">headfmt</span> <span class="o">=</span> <span class="s">"{1} [{0}]"</span><span class="o">.</span><span class="n">format</span>
<span class="p">):</span>
<span class="k">yield</span> <span class="bp">None</span><span class="p">,</span> <span class="n">headfmt</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mode</span><span class="p">)</span>
<span class="nd">@abstractproperty</span>
<div class="viewcode-block" id="ALock.mode"><a class="viewcode-back" href="../../reference.html#postgresql.alock.ALock.mode">[docs]</a> <span class="k">def</span> <span class="nf">mode</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> The mode of the lock class.</span>
<span class="sd"> """</span>
</div>
<span class="nd">@abstractproperty</span>
<span class="k">def</span> <span class="nf">__select_statements__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Implemented by subclasses to return the statements to try, acquire, and</span>
<span class="sd"> release the advisory lock.</span>
<span class="sd"> Returns a triple of callables where each callable takes two arguments,</span>
<span class="sd"> the lock-id pairs, and then the int8 lock-ids.</span>
<span class="sd"> ``(try, acquire, release)``.</span>
<span class="sd"> """</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">_split_lock_identifiers</span><span class="p">(</span><span class="n">idseq</span><span class="p">):</span>
<span class="c"># lame O(2)</span>
<span class="n">id_pairs</span> <span class="o">=</span> <span class="p">[</span>
<span class="nb">list</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">__class__</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">int</span> <span class="k">else</span> <span class="p">[</span><span class="bp">None</span><span class="p">,</span><span class="bp">None</span><span class="p">]</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">idseq</span>
<span class="p">]</span>
<span class="n">ids</span> <span class="o">=</span> <span class="p">[</span>
<span class="n">x</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">__class__</span> <span class="ow">is</span> <span class="nb">int</span> <span class="k">else</span> <span class="bp">None</span>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">idseq</span>
<span class="p">]</span>
<span class="k">return</span> <span class="p">(</span><span class="n">id_pairs</span><span class="p">,</span> <span class="n">ids</span><span class="p">)</span>
<div class="viewcode-block" id="ALock.acquire"><a class="viewcode-back" href="../../reference.html#postgresql.alock.ALock.acquire">[docs]</a> <span class="k">def</span> <span class="nf">acquire</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">blocking</span> <span class="o">=</span> <span class="bp">True</span><span class="p">,</span> <span class="nb">len</span> <span class="o">=</span> <span class="nb">len</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Acquire the locks using the configured identifiers.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="c"># _count is zero, so the locks need to be acquired.</span>
<span class="n">wait</span> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><span class="n">blocking</span><span class="p">)</span>
<span class="k">if</span> <span class="n">wait</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_acquire</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_id_pairs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ids</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="c"># grab the success of each lock id. if some were</span>
<span class="c"># unsuccessful, then the ones that were successful need to be</span>
<span class="c"># released.</span>
<span class="n">r</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_try</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_id_pairs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ids</span><span class="p">)</span>
<span class="c"># accumulate the identifiers that *did* lock</span>
<span class="n">release_seq</span> <span class="o">=</span> <span class="p">[</span>
<span class="nb">id</span> <span class="k">for</span> <span class="n">didlock</span><span class="p">,</span> <span class="nb">id</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">identifiers</span><span class="p">)</span> <span class="k">if</span> <span class="n">didlock</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="p">]</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">release_seq</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">identifiers</span><span class="p">):</span>
<span class="c"># some failed, so release the acquired and return False</span>
<span class="c">#</span>
<span class="c"># reverse in case there is another waiting for all.</span>
<span class="c"># that is, release last-to-first so that if another is waiting</span>
<span class="c"># on the same seq that it should be able to acquire all of</span>
<span class="c"># them once the contended lock is released.</span>
<span class="n">release_seq</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_release</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_split_lock_identifiers</span><span class="p">(</span><span class="n">release_seq</span><span class="p">))</span>
<span class="c"># unable to acquire all.</span>
<span class="k">return</span> <span class="bp">False</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">+</span> <span class="mi">1</span>
<span class="k">return</span> <span class="bp">True</span>
</div>
<span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">acquire</span><span class="p">()</span>
<span class="k">return</span> <span class="bp">self</span>
<div class="viewcode-block" id="ALock.release"><a class="viewcode-back" href="../../reference.html#postgresql.alock.ALock.release">[docs]</a> <span class="k">def</span> <span class="nf">release</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Release the locks using the configured identifiers.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o"><</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">"cannot release un-acquired lock"</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">closed</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="c"># if the database has been closed, or the count will</span>
<span class="c"># remain non-zero, there is no need to release.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_release</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_id_pairs</span><span class="p">),</span> <span class="nb">reversed</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_ids</span><span class="p">))</span>
<span class="c"># decrement the count nonetheless.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">-</span> <span class="mi">1</span>
</div>
<span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">typ</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">tb</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">release</span><span class="p">()</span>
<div class="viewcode-block" id="ALock.locked"><a class="viewcode-back" href="../../reference.html#postgresql.alock.ALock.locked">[docs]</a> <span class="k">def</span> <span class="nf">locked</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Whether the locks have been acquired. This method is sensitive to the</span>
<span class="sd"> connection's state. If the connection is closed, it will return False.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">closed</span><span class="p">)</span>
</div>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">state</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="s">'locked'</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">locked</span><span class="p">()</span> <span class="k">else</span> <span class="s">'unlocked'</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">database</span><span class="p">,</span> <span class="o">*</span><span class="n">identifiers</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Initialize the lock object to manage a sequence of advisory locks</span>
<span class="sd"> for use with the given database.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_count</span> <span class="o">=</span> <span class="mi">0</span>
<span class="bp">self</span><span class="o">.</span><span class="n">connection</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">database</span> <span class="o">=</span> <span class="n">database</span>
<span class="bp">self</span><span class="o">.</span><span class="n">identifiers</span> <span class="o">=</span> <span class="n">identifiers</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_id_pairs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ids</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_split_lock_identifiers</span><span class="p">(</span><span class="n">identifiers</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_try</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_acquire</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_release</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__select_statements__</span><span class="p">()</span>
</div>
<span class="k">class</span> <span class="nc">ShareLock</span><span class="p">(</span><span class="n">ALock</span><span class="p">):</span>
<span class="n">mode</span> <span class="o">=</span> <span class="s">'share'</span>
<span class="k">def</span> <span class="nf">__select_statements__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span>
<span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">sys</span><span class="o">.</span><span class="n">try_advisory_shared</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">sys</span><span class="o">.</span><span class="n">acquire_advisory_shared</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">sys</span><span class="o">.</span><span class="n">release_advisory_shared</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">class</span> <span class="nc">ExclusiveLock</span><span class="p">(</span><span class="n">ALock</span><span class="p">):</span>
<span class="n">mode</span> <span class="o">=</span> <span class="s">'exclusive'</span>
<span class="k">def</span> <span class="nf">__select_statements__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span>
<span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">sys</span><span class="o">.</span><span class="n">try_advisory_exclusive</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">sys</span><span class="o">.</span><span class="n">acquire_advisory_exclusive</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="o">.</span><span class="n">sys</span><span class="o">.</span><span class="n">release_advisory_exclusive</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<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><a href="../../index.html">py-postgresql 1.1.0 documentation</a> »</li>
<li><a href="../index.html" >Module code</a> »</li>
<li><a href="../postgresql.html" >postgresql</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright Python+Postgres.
Last updated on Mar 31, 2014.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
</div>
</body>
</html>
|