| |
| |
| <!DOCTYPE html> |
| <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> |
| <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> |
| <head> |
| <meta charset="utf-8"> |
| |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| |
| <title>apache_beam.utils.shared module — Apache Beam documentation</title> |
| |
| |
| |
| |
| |
| |
| |
| |
| <script type="text/javascript" src="_static/js/modernizr.min.js"></script> |
| |
| |
| <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></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/language_data.js"></script> |
| |
| <script type="text/javascript" src="_static/js/theme.js"></script> |
| |
| |
| |
| |
| <link rel="stylesheet" href="_static/css/theme.css" type="text/css" /> |
| <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> |
| <link rel="index" title="Index" href="genindex.html" /> |
| <link rel="search" title="Search" href="search.html" /> |
| <link rel="next" title="apache_beam.utils.subprocess_server module" href="apache_beam.utils.subprocess_server.html" /> |
| <link rel="prev" title="apache_beam.utils.retry module" href="apache_beam.utils.retry.html" /> |
| </head> |
| |
| <body class="wy-body-for-nav"> |
| |
| |
| <div class="wy-grid-for-nav"> |
| |
| <nav data-toggle="wy-nav-shift" class="wy-nav-side"> |
| <div class="wy-side-scroll"> |
| <div class="wy-side-nav-search" > |
| |
| |
| |
| <a href="index.html" class="icon icon-home"> Apache Beam |
| |
| |
| |
| </a> |
| |
| |
| |
| |
| |
| |
| |
| <div role="search"> |
| <form id="rtd-search-form" class="wy-form" action="search.html" method="get"> |
| <input type="text" name="q" placeholder="Search docs" /> |
| <input type="hidden" name="check_keywords" value="yes" /> |
| <input type="hidden" name="area" value="default" /> |
| </form> |
| </div> |
| |
| |
| </div> |
| |
| <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> |
| |
| |
| |
| |
| |
| |
| <ul class="current"> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.coders.html">apache_beam.coders package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.dataframe.html">apache_beam.dataframe package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.internal.html">apache_beam.internal package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.io.html">apache_beam.io package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.metrics.html">apache_beam.metrics package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.ml.html">apache_beam.ml package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.options.html">apache_beam.options package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.portability.html">apache_beam.portability package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.runners.html">apache_beam.runners package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.testing.html">apache_beam.testing package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.tools.html">apache_beam.tools package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.transforms.html">apache_beam.transforms package</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.typehints.html">apache_beam.typehints package</a></li> |
| <li class="toctree-l1 current"><a class="reference internal" href="apache_beam.utils.html">apache_beam.utils package</a><ul class="current"> |
| <li class="toctree-l2 current"><a class="reference internal" href="apache_beam.utils.html#submodules">Submodules</a><ul class="current"> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.annotations.html">apache_beam.utils.annotations module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.histogram.html">apache_beam.utils.histogram module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.interactive_utils.html">apache_beam.utils.interactive_utils module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.plugin.html">apache_beam.utils.plugin module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.processes.html">apache_beam.utils.processes module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.profiler.html">apache_beam.utils.profiler module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.proto_utils.html">apache_beam.utils.proto_utils module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.retry.html">apache_beam.utils.retry module</a></li> |
| <li class="toctree-l3 current"><a class="current reference internal" href="#">apache_beam.utils.shared module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.subprocess_server.html">apache_beam.utils.subprocess_server module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.thread_pool_executor.html">apache_beam.utils.thread_pool_executor module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.timestamp.html">apache_beam.utils.timestamp module</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="apache_beam.utils.urns.html">apache_beam.utils.urns module</a></li> |
| </ul> |
| </li> |
| </ul> |
| </li> |
| </ul> |
| <ul> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.error.html">apache_beam.error module</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.pipeline.html">apache_beam.pipeline module</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.pvalue.html">apache_beam.pvalue module</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="apache_beam.version.html">apache_beam.version module</a></li> |
| </ul> |
| |
| |
| |
| </div> |
| </div> |
| </nav> |
| |
| <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> |
| |
| |
| <nav class="wy-nav-top" aria-label="top navigation"> |
| |
| <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| <a href="index.html">Apache Beam</a> |
| |
| </nav> |
| |
| |
| <div class="wy-nav-content"> |
| |
| <div class="rst-content"> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <div role="navigation" aria-label="breadcrumbs navigation"> |
| |
| <ul class="wy-breadcrumbs"> |
| |
| <li><a href="index.html">Docs</a> »</li> |
| |
| <li><a href="apache_beam.utils.html">apache_beam.utils package</a> »</li> |
| |
| <li>apache_beam.utils.shared module</li> |
| |
| |
| <li class="wy-breadcrumbs-aside"> |
| |
| |
| <a href="_sources/apache_beam.utils.shared.rst.txt" rel="nofollow"> View page source</a> |
| |
| |
| </li> |
| |
| </ul> |
| |
| |
| <hr/> |
| </div> |
| <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article"> |
| <div itemprop="articleBody"> |
| |
| <div class="section" id="module-apache_beam.utils.shared"> |
| <span id="apache-beam-utils-shared-module"></span><h1>apache_beam.utils.shared module<a class="headerlink" href="#module-apache_beam.utils.shared" title="Permalink to this headline">¶</a></h1> |
| <p>Shared class.</p> |
| <p>Shared class for managing a single instance of an object shared by multiple |
| threads within the same process. Shared is a serializable object that can be |
| shared by all threads of each worker process, which will be initialized as |
| necessary by calls to acquire.</p> |
| <p>Example usage:</p> |
| <p>To share a very large list across all threads of each worker in a DoFn:</p> |
| <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">GetNthStringFn</span><span class="p">(</span><span class="n">beam</span><span class="o">.</span><span class="n">DoFn</span><span class="p">):</span> |
| <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">shared_handle</span><span class="p">):</span> |
| <span class="bp">self</span><span class="o">.</span><span class="n">_shared_handle</span> <span class="o">=</span> <span class="n">shared_handle</span> |
| |
| <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">):</span> |
| <span class="k">def</span> <span class="nf">initialize_list</span><span class="p">():</span> |
| <span class="c1"># Build the giant initial list.</span> |
| <span class="k">return</span> <span class="p">[</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1000000</span><span class="p">)]</span> |
| |
| <span class="n">giant_list</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_shared_handle</span><span class="o">.</span><span class="n">acquire</span><span class="p">(</span><span class="n">initialize_list</span><span class="p">)</span> |
| <span class="k">yield</span> <span class="n">giant_list</span><span class="p">[</span><span class="n">element</span><span class="p">]</span> |
| |
| <span class="n">p</span> <span class="o">=</span> <span class="n">beam</span><span class="o">.</span><span class="n">Pipeline</span><span class="p">()</span> |
| <span class="n">shared_handle</span> <span class="o">=</span> <span class="n">shared</span><span class="o">.</span><span class="n">Shared</span><span class="p">()</span> |
| <span class="p">(</span><span class="n">p</span> <span class="o">|</span> <span class="n">beam</span><span class="o">.</span><span class="n">Create</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">])</span> |
| <span class="o">|</span> <span class="n">beam</span><span class="o">.</span><span class="n">ParDo</span><span class="p">(</span><span class="n">GetNthStringFn</span><span class="p">(</span><span class="n">shared_handle</span><span class="p">)))</span> |
| </pre></div> |
| </div> |
| <p>Real-world uses will typically involve using a side-input to a DoFn to |
| initialize the shared resource in a way that can’t be done with just its |
| constructor:</p> |
| <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">RainbowTableLookupFn</span><span class="p">(</span><span class="n">beam</span><span class="o">.</span><span class="n">DoFn</span><span class="p">):</span> |
| <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">shared_handle</span><span class="p">):</span> |
| <span class="bp">self</span><span class="o">.</span><span class="n">_shared_handle</span> <span class="o">=</span> <span class="n">shared_handle</span> |
| |
| <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">element</span><span class="p">,</span> <span class="n">table_elements</span><span class="p">):</span> |
| <span class="k">def</span> <span class="nf">construct_table</span><span class="p">():</span> |
| <span class="c1"># Construct the rainbow table from the table elements.</span> |
| <span class="c1"># The table contains lines in the form "string::hash"</span> |
| <span class="n">result</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span> |
| <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">table_elements</span><span class="p">:</span> |
| <span class="n">result</span><span class="p">[</span><span class="n">value</span><span class="p">]</span> <span class="o">=</span> <span class="n">key</span> |
| <span class="k">return</span> <span class="n">result</span> |
| |
| <span class="n">rainbow_table</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_shared_handle</span><span class="o">.</span><span class="n">acquire</span><span class="p">(</span><span class="n">construct_table</span><span class="p">)</span> |
| <span class="n">unhashed_str</span> <span class="o">=</span> <span class="n">rainbow_table</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">element</span><span class="p">)</span> |
| <span class="k">if</span> <span class="n">unhashed_str</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span> |
| <span class="k">yield</span> <span class="n">unhashed_str</span> |
| |
| <span class="n">p</span> <span class="o">=</span> <span class="n">beam</span><span class="o">.</span><span class="n">Pipeline</span><span class="p">()</span> |
| <span class="n">shared_handle</span> <span class="o">=</span> <span class="n">shared</span><span class="o">.</span><span class="n">Shared</span><span class="p">()</span> |
| <span class="n">reverse_hash_table</span> <span class="o">=</span> <span class="n">p</span> <span class="o">|</span> <span class="s2">"ReverseHashTable"</span> <span class="o">>></span> <span class="n">beam</span><span class="o">.</span><span class="n">Create</span><span class="p">([</span> |
| <span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'0cc175b9c0f1b6a831c399e269772661'</span><span class="p">),</span> |
| <span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="s1">'92eb5ffee6ae2fec3ad71c777531578f'</span><span class="p">),</span> |
| <span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="s1">'4a8a08f09d37b73795649038408b5f33'</span><span class="p">),</span> |
| <span class="p">(</span><span class="s1">'d'</span><span class="p">,</span> <span class="s1">'8277e0910d750195b448797616e091ad'</span><span class="p">)])</span> |
| <span class="n">unhashed</span> <span class="o">=</span> <span class="p">(</span><span class="n">p</span> |
| <span class="o">|</span> <span class="s1">'Hashes'</span> <span class="o">>></span> <span class="n">beam</span><span class="o">.</span><span class="n">Create</span><span class="p">([</span> |
| <span class="s1">'0cc175b9c0f1b6a831c399e269772661'</span><span class="p">,</span> |
| <span class="s1">'8277e0910d750195b448797616e091ad'</span><span class="p">])</span> |
| <span class="o">|</span> <span class="s1">'Unhash'</span> <span class="o">>></span> <span class="n">beam</span><span class="o">.</span><span class="n">ParDo</span><span class="p">(</span> |
| <span class="n">RainbowTableLookupFn</span><span class="p">(</span><span class="n">shared_handle</span><span class="p">),</span> <span class="n">reverse_hash_table</span><span class="p">))</span> |
| </pre></div> |
| </div> |
| <dl class="class"> |
| <dt id="apache_beam.utils.shared.Shared"> |
| <em class="property">class </em><code class="descclassname">apache_beam.utils.shared.</code><code class="descname">Shared</code><a class="reference internal" href="_modules/apache_beam/utils/shared.html#Shared"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#apache_beam.utils.shared.Shared" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Bases: <a class="reference external" href="https://docs.python.org/3/library/functions.html#object" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">object</span></code></a></p> |
| <p>Handle for managing shared per-process objects.</p> |
| <p>Each instance of a Shared object represents a distinct handle to a distinct |
| object. Example usage is described in the file comment of shared.py.</p> |
| <p>This object has the following limitations: |
| * A shared object won’t be GC’ed if there isn’t another acquire called for |
| a different shared object. |
| * Each stage can only use exactly one Shared token, otherwise only one |
| Shared token, <em>NOT NECESSARILY THE LATEST</em>, will be “kept-alive”. |
| * If there are two different stages using separate Shared tokens, but which |
| get fused together, only one Shared token will be “kept-alive”.</p> |
| <p>(See documentation of _SharedMap for details.)</p> |
| <dl class="method"> |
| <dt id="apache_beam.utils.shared.Shared.acquire"> |
| <code class="descname">acquire</code><span class="sig-paren">(</span><em>constructor_fn</em>, <em>tag=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/apache_beam/utils/shared.html#Shared.acquire"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#apache_beam.utils.shared.Shared.acquire" title="Permalink to this definition">¶</a></dt> |
| <dd><p>Acquire a reference to the object associated with this Shared handle.</p> |
| <table class="docutils field-list" frame="void" rules="none"> |
| <col class="field-name" /> |
| <col class="field-body" /> |
| <tbody valign="top"> |
| <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| <li><strong>constructor_fn</strong> – function that initialises / constructs the object if not |
| present in the cache. This function should take no arguments. It should |
| return an initialised object, or None if the object could not be |
| initialised / constructed.</li> |
| <li><strong>tag</strong> – an optional indentifier to store with the cached object. If |
| subsequent calls to acquire use different tags, the object will be |
| reloaded rather than returned from cache.</li> |
| </ul> |
| </td> |
| </tr> |
| <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A reference to an initialised object, either from the cache, or |
| newly-constructed.</p> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| </dd></dl> |
| |
| </dd></dl> |
| |
| </div> |
| |
| |
| </div> |
| |
| </div> |
| <footer> |
| |
| <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> |
| |
| <a href="apache_beam.utils.subprocess_server.html" class="btn btn-neutral float-right" title="apache_beam.utils.subprocess_server module" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a> |
| |
| |
| <a href="apache_beam.utils.retry.html" class="btn btn-neutral float-left" title="apache_beam.utils.retry module" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a> |
| |
| </div> |
| |
| |
| <hr/> |
| |
| <div role="contentinfo"> |
| <p> |
| © Copyright |
| |
| </p> |
| </div> |
| Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| |
| </footer> |
| |
| </div> |
| </div> |
| |
| </section> |
| |
| </div> |
| |
| |
| |
| <script type="text/javascript"> |
| jQuery(function () { |
| SphinxRtdTheme.Navigation.enable(true); |
| }); |
| </script> |
| |
| |
| |
| |
| |
| |
| </body> |
| </html> |