future.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>grpc.framework.foundation.future &mdash; grpcio 1.0.4 documentation</title>
  8. <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  9. <link rel="index" title="Index"
  10. href="../../../../genindex.html"/>
  11. <link rel="search" title="Search" href="../../../../search.html"/>
  12. <link rel="top" title="grpcio 1.0.4 documentation" href="../../../../index.html"/>
  13. <link rel="up" title="grpc" href="../../../grpc.html"/>
  14. <script src="../../../../_static/js/modernizr.min.js"></script>
  15. </head>
  16. <body class="wy-body-for-nav" role="document">
  17. <div class="wy-grid-for-nav">
  18. <nav data-toggle="wy-nav-shift" class="wy-nav-side">
  19. <div class="wy-side-scroll">
  20. <div class="wy-side-nav-search">
  21. <a href="../../../../index.html" class="icon icon-home"> grpcio
  22. </a>
  23. <div class="version">
  24. 1.0.4
  25. </div>
  26. <div role="search">
  27. <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
  28. <input type="text" name="q" placeholder="Search docs" />
  29. <input type="hidden" name="check_keywords" value="yes" />
  30. <input type="hidden" name="area" value="default" />
  31. </form>
  32. </div>
  33. </div>
  34. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  35. <p class="caption"><span class="caption-text">Contents:</span></p>
  36. <ul>
  37. <li class="toctree-l1"><a class="reference internal" href="../../../../grpc.html">grpc package</a></li>
  38. <li class="toctree-l1"><a class="reference internal" href="../../../../grpc.html#glossary">Glossary</a></li>
  39. </ul>
  40. </div>
  41. </div>
  42. </nav>
  43. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  44. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  45. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  46. <a href="../../../../index.html">grpcio</a>
  47. </nav>
  48. <div class="wy-nav-content">
  49. <div class="rst-content">
  50. <div role="navigation" aria-label="breadcrumbs navigation">
  51. <ul class="wy-breadcrumbs">
  52. <li><a href="../../../../index.html">Docs</a> &raquo;</li>
  53. <li><a href="../../../index.html">Module code</a> &raquo;</li>
  54. <li><a href="../../../grpc.html">grpc</a> &raquo;</li>
  55. <li>grpc.framework.foundation.future</li>
  56. <li class="wy-breadcrumbs-aside">
  57. </li>
  58. </ul>
  59. <hr/>
  60. </div>
  61. <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
  62. <div itemprop="articleBody">
  63. <h1>Source code for grpc.framework.foundation.future</h1><div class="highlight"><pre>
  64. <span></span><span class="c1"># Copyright 2015, Google Inc.</span>
  65. <span class="c1"># All rights reserved.</span>
  66. <span class="c1">#</span>
  67. <span class="c1"># Redistribution and use in source and binary forms, with or without</span>
  68. <span class="c1"># modification, are permitted provided that the following conditions are</span>
  69. <span class="c1"># met:</span>
  70. <span class="c1">#</span>
  71. <span class="c1"># * Redistributions of source code must retain the above copyright</span>
  72. <span class="c1"># notice, this list of conditions and the following disclaimer.</span>
  73. <span class="c1"># * Redistributions in binary form must reproduce the above</span>
  74. <span class="c1"># copyright notice, this list of conditions and the following disclaimer</span>
  75. <span class="c1"># in the documentation and/or other materials provided with the</span>
  76. <span class="c1"># distribution.</span>
  77. <span class="c1"># * Neither the name of Google Inc. nor the names of its</span>
  78. <span class="c1"># contributors may be used to endorse or promote products derived from</span>
  79. <span class="c1"># this software without specific prior written permission.</span>
  80. <span class="c1">#</span>
  81. <span class="c1"># THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
  82. <span class="c1"># &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
  83. <span class="c1"># LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
  84. <span class="c1"># A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span>
  85. <span class="c1"># OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
  86. <span class="c1"># SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
  87. <span class="c1"># LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span>
  88. <span class="c1"># DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span>
  89. <span class="c1"># THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
  90. <span class="c1"># (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
  91. <span class="c1"># OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
  92. <span class="sd">&quot;&quot;&quot;A Future interface.</span>
  93. <span class="sd">Python doesn&#39;t have a Future interface in its standard library. In the absence</span>
  94. <span class="sd">of such a standard, three separate, incompatible implementations</span>
  95. <span class="sd">(concurrent.futures.Future, ndb.Future, and asyncio.Future) have appeared. This</span>
  96. <span class="sd">interface attempts to be as compatible as possible with</span>
  97. <span class="sd">concurrent.futures.Future. From ndb.Future it adopts a traceback-object accessor</span>
  98. <span class="sd">method.</span>
  99. <span class="sd">Unlike the concrete and implemented Future classes listed above, the Future</span>
  100. <span class="sd">class defined in this module is an entirely abstract interface that anyone may</span>
  101. <span class="sd">implement and use.</span>
  102. <span class="sd">The one known incompatibility between this interface and the interface of</span>
  103. <span class="sd">concurrent.futures.Future is that this interface defines its own CancelledError</span>
  104. <span class="sd">and TimeoutError exceptions rather than raising the implementation-private</span>
  105. <span class="sd">concurrent.futures._base.CancelledError and the</span>
  106. <span class="sd">built-in-but-only-in-3.3-and-later TimeoutError.</span>
  107. <span class="sd">&quot;&quot;&quot;</span>
  108. <span class="kn">import</span> <span class="nn">abc</span>
  109. <span class="kn">import</span> <span class="nn">six</span>
  110. <div class="viewcode-block" id="TimeoutError"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.TimeoutError">[docs]</a><span class="k">class</span> <span class="nc">TimeoutError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  111. <span class="sd">&quot;&quot;&quot;Indicates that a particular call timed out.&quot;&quot;&quot;</span></div>
  112. <div class="viewcode-block" id="CancelledError"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.CancelledError">[docs]</a><span class="k">class</span> <span class="nc">CancelledError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
  113. <span class="sd">&quot;&quot;&quot;Indicates that the computation underlying a Future was cancelled.&quot;&quot;&quot;</span></div>
  114. <div class="viewcode-block" id="Future"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future">[docs]</a><span class="k">class</span> <span class="nc">Future</span><span class="p">(</span><span class="n">six</span><span class="o">.</span><span class="n">with_metaclass</span><span class="p">(</span><span class="n">abc</span><span class="o">.</span><span class="n">ABCMeta</span><span class="p">)):</span>
  115. <span class="sd">&quot;&quot;&quot;A representation of a computation in another control flow.</span>
  116. <span class="sd"> Computations represented by a Future may be yet to be begun, may be ongoing,</span>
  117. <span class="sd"> or may have already completed.</span>
  118. <span class="sd"> &quot;&quot;&quot;</span>
  119. <span class="c1"># NOTE(nathaniel): This isn&#39;t the return type that I would want to have if it</span>
  120. <span class="c1"># were up to me. Were this interface being written from scratch, the return</span>
  121. <span class="c1"># type of this method would probably be a sum type like:</span>
  122. <span class="c1">#</span>
  123. <span class="c1"># NOT_COMMENCED</span>
  124. <span class="c1"># COMMENCED_AND_NOT_COMPLETED</span>
  125. <span class="c1"># PARTIAL_RESULT&lt;Partial_Result_Type&gt;</span>
  126. <span class="c1"># COMPLETED&lt;Result_Type&gt;</span>
  127. <span class="c1"># UNCANCELLABLE</span>
  128. <span class="c1"># NOT_IMMEDIATELY_DETERMINABLE</span>
  129. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  130. <div class="viewcode-block" id="Future.cancel"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.cancel">[docs]</a> <span class="k">def</span> <span class="nf">cancel</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  131. <span class="sd">&quot;&quot;&quot;Attempts to cancel the computation.</span>
  132. <span class="sd"> This method does not block.</span>
  133. <span class="sd"> Returns:</span>
  134. <span class="sd"> True if the computation has not yet begun, will not be allowed to take</span>
  135. <span class="sd"> place, and determination of both was possible without blocking. False</span>
  136. <span class="sd"> under all other circumstances including but not limited to the</span>
  137. <span class="sd"> computation&#39;s already having begun, the computation&#39;s already having</span>
  138. <span class="sd"> finished, and the computation&#39;s having been scheduled for execution on a</span>
  139. <span class="sd"> remote system for which a determination of whether or not it commenced</span>
  140. <span class="sd"> before being cancelled cannot be made without blocking.</span>
  141. <span class="sd"> &quot;&quot;&quot;</span>
  142. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
  143. <span class="c1"># NOTE(nathaniel): Here too this isn&#39;t the return type that I&#39;d want this</span>
  144. <span class="c1"># method to have if it were up to me. I think I&#39;d go with another sum type</span>
  145. <span class="c1"># like:</span>
  146. <span class="c1">#</span>
  147. <span class="c1"># NOT_CANCELLED (this object&#39;s cancel method hasn&#39;t been called)</span>
  148. <span class="c1"># NOT_COMMENCED</span>
  149. <span class="c1"># COMMENCED_AND_NOT_COMPLETED</span>
  150. <span class="c1"># PARTIAL_RESULT&lt;Partial_Result_Type&gt;</span>
  151. <span class="c1"># COMPLETED&lt;Result_Type&gt;</span>
  152. <span class="c1"># UNCANCELLABLE</span>
  153. <span class="c1"># NOT_IMMEDIATELY_DETERMINABLE</span>
  154. <span class="c1">#</span>
  155. <span class="c1"># Notice how giving the cancel method the right semantics obviates most</span>
  156. <span class="c1"># reasons for this method to exist.</span>
  157. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  158. <div class="viewcode-block" id="Future.cancelled"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.cancelled">[docs]</a> <span class="k">def</span> <span class="nf">cancelled</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  159. <span class="sd">&quot;&quot;&quot;Describes whether the computation was cancelled.</span>
  160. <span class="sd"> This method does not block.</span>
  161. <span class="sd"> Returns:</span>
  162. <span class="sd"> True if the computation was cancelled any time before its result became</span>
  163. <span class="sd"> immediately available. False under all other circumstances including but</span>
  164. <span class="sd"> not limited to this object&#39;s cancel method not having been called and</span>
  165. <span class="sd"> the computation&#39;s result having become immediately available.</span>
  166. <span class="sd"> &quot;&quot;&quot;</span>
  167. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
  168. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  169. <div class="viewcode-block" id="Future.running"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.running">[docs]</a> <span class="k">def</span> <span class="nf">running</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  170. <span class="sd">&quot;&quot;&quot;Describes whether the computation is taking place.</span>
  171. <span class="sd"> This method does not block.</span>
  172. <span class="sd"> Returns:</span>
  173. <span class="sd"> True if the computation is scheduled to take place in the future or is</span>
  174. <span class="sd"> taking place now, or False if the computation took place in the past or</span>
  175. <span class="sd"> was cancelled.</span>
  176. <span class="sd"> &quot;&quot;&quot;</span>
  177. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
  178. <span class="c1"># NOTE(nathaniel): These aren&#39;t quite the semantics I&#39;d like here either. I</span>
  179. <span class="c1"># would rather this only returned True in cases in which the underlying</span>
  180. <span class="c1"># computation completed successfully. A computation&#39;s having been cancelled</span>
  181. <span class="c1"># conflicts with considering that computation &quot;done&quot;.</span>
  182. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  183. <div class="viewcode-block" id="Future.done"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.done">[docs]</a> <span class="k">def</span> <span class="nf">done</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  184. <span class="sd">&quot;&quot;&quot;Describes whether the computation has taken place.</span>
  185. <span class="sd"> This method does not block.</span>
  186. <span class="sd"> Returns:</span>
  187. <span class="sd"> True if the computation is known to have either completed or have been</span>
  188. <span class="sd"> unscheduled or interrupted. False if the computation may possibly be</span>
  189. <span class="sd"> executing or scheduled to execute later.</span>
  190. <span class="sd"> &quot;&quot;&quot;</span>
  191. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
  192. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  193. <div class="viewcode-block" id="Future.result"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.result">[docs]</a> <span class="k">def</span> <span class="nf">result</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  194. <span class="sd">&quot;&quot;&quot;Accesses the outcome of the computation or raises its exception.</span>
  195. <span class="sd"> This method may return immediately or may block.</span>
  196. <span class="sd"> Args:</span>
  197. <span class="sd"> timeout: The length of time in seconds to wait for the computation to</span>
  198. <span class="sd"> finish or be cancelled, or None if this method should block until the</span>
  199. <span class="sd"> computation has finished or is cancelled no matter how long that takes.</span>
  200. <span class="sd"> Returns:</span>
  201. <span class="sd"> The return value of the computation.</span>
  202. <span class="sd"> Raises:</span>
  203. <span class="sd"> TimeoutError: If a timeout value is passed and the computation does not</span>
  204. <span class="sd"> terminate within the allotted time.</span>
  205. <span class="sd"> CancelledError: If the computation was cancelled.</span>
  206. <span class="sd"> Exception: If the computation raised an exception, this call will raise</span>
  207. <span class="sd"> the same exception.</span>
  208. <span class="sd"> &quot;&quot;&quot;</span>
  209. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
  210. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  211. <div class="viewcode-block" id="Future.exception"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.exception">[docs]</a> <span class="k">def</span> <span class="nf">exception</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  212. <span class="sd">&quot;&quot;&quot;Return the exception raised by the computation.</span>
  213. <span class="sd"> This method may return immediately or may block.</span>
  214. <span class="sd"> Args:</span>
  215. <span class="sd"> timeout: The length of time in seconds to wait for the computation to</span>
  216. <span class="sd"> terminate or be cancelled, or None if this method should block until</span>
  217. <span class="sd"> the computation is terminated or is cancelled no matter how long that</span>
  218. <span class="sd"> takes.</span>
  219. <span class="sd"> Returns:</span>
  220. <span class="sd"> The exception raised by the computation, or None if the computation did</span>
  221. <span class="sd"> not raise an exception.</span>
  222. <span class="sd"> Raises:</span>
  223. <span class="sd"> TimeoutError: If a timeout value is passed and the computation does not</span>
  224. <span class="sd"> terminate within the allotted time.</span>
  225. <span class="sd"> CancelledError: If the computation was cancelled.</span>
  226. <span class="sd"> &quot;&quot;&quot;</span>
  227. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
  228. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  229. <div class="viewcode-block" id="Future.traceback"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.traceback">[docs]</a> <span class="k">def</span> <span class="nf">traceback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
  230. <span class="sd">&quot;&quot;&quot;Access the traceback of the exception raised by the computation.</span>
  231. <span class="sd"> This method may return immediately or may block.</span>
  232. <span class="sd"> Args:</span>
  233. <span class="sd"> timeout: The length of time in seconds to wait for the computation to</span>
  234. <span class="sd"> terminate or be cancelled, or None if this method should block until</span>
  235. <span class="sd"> the computation is terminated or is cancelled no matter how long that</span>
  236. <span class="sd"> takes.</span>
  237. <span class="sd"> Returns:</span>
  238. <span class="sd"> The traceback of the exception raised by the computation, or None if the</span>
  239. <span class="sd"> computation did not raise an exception.</span>
  240. <span class="sd"> Raises:</span>
  241. <span class="sd"> TimeoutError: If a timeout value is passed and the computation does not</span>
  242. <span class="sd"> terminate within the allotted time.</span>
  243. <span class="sd"> CancelledError: If the computation was cancelled.</span>
  244. <span class="sd"> &quot;&quot;&quot;</span>
  245. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div>
  246. <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
  247. <div class="viewcode-block" id="Future.add_done_callback"><a class="viewcode-back" href="../../../../grpc.framework.foundation.html#grpc.framework.foundation.future.Future.add_done_callback">[docs]</a> <span class="k">def</span> <span class="nf">add_done_callback</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fn</span><span class="p">):</span>
  248. <span class="sd">&quot;&quot;&quot;Adds a function to be called at completion of the computation.</span>
  249. <span class="sd"> The callback will be passed this Future object describing the outcome of</span>
  250. <span class="sd"> the computation.</span>
  251. <span class="sd"> If the computation has already completed, the callback will be called</span>
  252. <span class="sd"> immediately.</span>
  253. <span class="sd"> Args:</span>
  254. <span class="sd"> fn: A callable taking this Future object as its single parameter.</span>
  255. <span class="sd"> &quot;&quot;&quot;</span>
  256. <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">()</span></div></div>
  257. </pre></div>
  258. </div>
  259. </div>
  260. <footer>
  261. <hr/>
  262. <div role="contentinfo">
  263. <p>
  264. &copy; Copyright 2016, Author.
  265. </p>
  266. </div>
  267. Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
  268. </footer>
  269. </div>
  270. </div>
  271. </section>
  272. </div>
  273. <script type="text/javascript">
  274. var DOCUMENTATION_OPTIONS = {
  275. URL_ROOT:'../../../../',
  276. VERSION:'1.0.4',
  277. COLLAPSE_INDEX:false,
  278. FILE_SUFFIX:'.html',
  279. HAS_SOURCE: true
  280. };
  281. </script>
  282. <script type="text/javascript" src="../../../../_static/jquery.js"></script>
  283. <script type="text/javascript" src="../../../../_static/underscore.js"></script>
  284. <script type="text/javascript" src="../../../../_static/doctools.js"></script>
  285. <script type="text/javascript" src="../../../../_static/js/theme.js"></script>
  286. <script type="text/javascript">
  287. jQuery(function () {
  288. SphinxRtdTheme.StickyNav.enable();
  289. });
  290. </script>
  291. </body>
  292. </html>