blob: bc16c5b305eb6836fc0fb42f07b0337cc97bdc9c [file] [log] [blame]
<!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 &mdash; Apache Beam 2.47.0 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 async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></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" />
</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 class="version">
2.47.0
</div>
<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>
<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.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.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"><a class="reference internal" href="../../../apache_beam.utils.html">apache_beam.utils package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../apache_beam.yaml.html">apache_beam.yaml package</a></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>
</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> &raquo;</li>
<li><a href="../../index.html">Module code</a> &raquo;</li>
<li>apache_beam.utils.shared</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for apache_beam.utils.shared</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c1"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c1"># this work for additional information regarding copyright ownership.</span>
<span class="c1"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c1"># (the &quot;License&quot;); you may not use this file except in compliance with</span>
<span class="c1"># the License. You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>
<span class="sd">&quot;&quot;&quot;Shared class.</span>
<span class="sd">Shared is a helper class for managing a single instance of an object</span>
<span class="sd">shared by multiple threads within the same process. Instances of Shared</span>
<span class="sd">are serializable objects that can be shared by all threads of each worker</span>
<span class="sd">process. A Shared object encapsulates a weak reference to a singleton</span>
<span class="sd">instance of the shared resource. The singleton is lazily initialized by</span>
<span class="sd">calls to Shared.acquire().</span>
<span class="sd">Example usage:</span>
<span class="sd">To share a very large list across all threads of each worker in a DoFn::</span>
<span class="sd"> # Several built-in types such as list and dict do not directly support weak</span>
<span class="sd"> # references but can add support through subclassing:</span>
<span class="sd"> # https://docs.python.org/3/library/weakref.html</span>
<span class="sd"> class WeakRefList(list):</span>
<span class="sd"> pass</span>
<span class="sd"> class GetNthStringFn(beam.DoFn):</span>
<span class="sd"> def __init__(self):</span>
<span class="sd"> self._shared_handle = shared.Shared()</span>
<span class="sd"> def setup(self):</span>
<span class="sd"> # setup is a good place to initialize transient in-memory resources.</span>
<span class="sd"> def initialize_list():</span>
<span class="sd"> # Build the giant initial list.</span>
<span class="sd"> return WeakRefList([str(i) for i in range(1000000)])</span>
<span class="sd"> self._giant_list = self._shared_handle.acquire(initialize_list)</span>
<span class="sd"> def process(self, element):</span>
<span class="sd"> yield self._giant_list[element]</span>
<span class="sd"> p = beam.Pipeline()</span>
<span class="sd"> (p | beam.Create([2, 4, 6, 8])</span>
<span class="sd"> | beam.ParDo(GetNthStringFn()))</span>
<span class="sd">Real-world uses will typically involve using a side-input to a DoFn to</span>
<span class="sd">initialize the shared resource in a way that can&#39;t be done with just its</span>
<span class="sd">constructor::</span>
<span class="sd"> class RainbowTableLookupFn(beam.DoFn):</span>
<span class="sd"> def __init__(self):</span>
<span class="sd"> self._shared_handle = shared.Shared()</span>
<span class="sd"> def process(self, element, table_elements):</span>
<span class="sd"> def construct_table():</span>
<span class="sd"> # Construct the rainbow table from the table elements.</span>
<span class="sd"> # The table contains lines in the form &quot;string::hash&quot;</span>
<span class="sd"> result = {}</span>
<span class="sd"> for key, value in table_elements:</span>
<span class="sd"> result[value] = key</span>
<span class="sd"> return result</span>
<span class="sd"> rainbow_table = self._shared_handle.acquire(construct_table)</span>
<span class="sd"> unhashed_str = rainbow_table.get(element)</span>
<span class="sd"> if unhashed_str is not None:</span>
<span class="sd"> yield unhashed_str</span>
<span class="sd"> p = beam.Pipeline()</span>
<span class="sd"> reverse_hash_table = p | &quot;ReverseHashTable&quot; &gt;&gt; beam.Create([</span>
<span class="sd"> (&#39;a&#39;, &#39;0cc175b9c0f1b6a831c399e269772661&#39;),</span>
<span class="sd"> (&#39;b&#39;, &#39;92eb5ffee6ae2fec3ad71c777531578f&#39;),</span>
<span class="sd"> (&#39;c&#39;, &#39;4a8a08f09d37b73795649038408b5f33&#39;),</span>
<span class="sd"> (&#39;d&#39;, &#39;8277e0910d750195b448797616e091ad&#39;)])</span>
<span class="sd"> unhashed = (p</span>
<span class="sd"> | &#39;Hashes&#39; &gt;&gt; beam.Create([</span>
<span class="sd"> &#39;0cc175b9c0f1b6a831c399e269772661&#39;,</span>
<span class="sd"> &#39;8277e0910d750195b448797616e091ad&#39;])</span>
<span class="sd"> | &#39;Unhash&#39; &gt;&gt; beam.ParDo(</span>
<span class="sd"> RainbowTableLookupFn(), reverse_hash_table))</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">uuid</span>
<span class="kn">import</span> <span class="nn">weakref</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Callable</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Text</span>
<span class="k">class</span> <span class="nc">_SharedControlBlock</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Wrapper class for holding objects in the SharedMap.</span>
<span class="sd"> We need this so we can call constructors for distinct Shared elements in the</span>
<span class="sd"> SharedMap concurrently.</span>
<span class="sd"> &quot;&quot;&quot;</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="bp">self</span><span class="o">.</span><span class="n">_lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_ref</span> <span class="o">=</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_tag</span> <span class="o">=</span> <span class="kc">None</span>
<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">constructor_fn</span><span class="p">,</span> <span class="c1"># type: Callable[[], Any]</span>
<span class="n">tag</span><span class="o">=</span><span class="kc">None</span> <span class="c1"># type: Any</span>
<span class="p">):</span>
<span class="c1"># type: (...) -&gt; Any</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Acquire a reference to the object this shared control block manages.</span>
<span class="sd"> Args:</span>
<span class="sd"> constructor_fn: function that initialises / constructs the object if not</span>
<span class="sd"> present in the cache. This function should take no arguments. It should</span>
<span class="sd"> return an initialised object, or None if the object could not be</span>
<span class="sd"> initialised / constructed.</span>
<span class="sd"> tag: an optional indentifier to store with the cached object. If</span>
<span class="sd"> subsequent calls to acquire use different tags, the object will be</span>
<span class="sd"> reloaded rather than returned from cache.</span>
<span class="sd"> Returns:</span>
<span class="sd"> An initialised object, either from a previous initialisation, or</span>
<span class="sd"> newly-constructed.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="p">:</span>
<span class="c1"># self._ref is None if this is a new control block.</span>
<span class="c1"># self._ref() is None if the weak reference was GCed.</span>
<span class="c1"># self._tag != tag if user specifies a new identifier</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ref</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ref</span><span class="p">()</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">_tag</span> <span class="o">!=</span> <span class="n">tag</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">constructor_fn</span><span class="p">()</span>
<span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_ref</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">ref</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_tag</span> <span class="o">=</span> <span class="n">tag</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_ref</span><span class="p">()</span>
<span class="k">return</span> <span class="n">result</span>
<span class="k">class</span> <span class="nc">_SharedMap</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Map for storing objects pointed to by Shared.</span>
<span class="sd"> The behaviour of SharedMap is as follows: when acquire is called, if the</span>
<span class="sd"> Shared object has already been initialised, we return the already-initialised</span>
<span class="sd"> copy. If not, we call the constructor_fn to construct it, and store it in</span>
<span class="sd"> the cache.</span>
<span class="sd"> One big caveat is this: we want to support cases where there is some delay</span>
<span class="sd"> between reacquistion of Shared objects, i.e. there may be a short period of</span>
<span class="sd"> time in which there are no references to the object before it is reacquired.</span>
<span class="sd"> This happens in various Beam runners (e.g. Dataflow runner): if we use a</span>
<span class="sd"> single thread for doing predictions with a large model, when the thread</span>
<span class="sd"> finishes its workitem, it will release the reference to the model. Since</span>
<span class="sd"> there&#39;s only a single thread, the model will have zero references to it</span>
<span class="sd"> and will be garbage collected. Shortly after this, the process receives a new</span>
<span class="sd"> workitem, creates a new thread, and attempts to reacquire the model. If we</span>
<span class="sd"> don&#39;t keep the model alive in between, the new thread will have to</span>
<span class="sd"> reinitialise the model from scratch.</span>
<span class="sd"> As such, we need to do some extra work to manage cached objects&#39; lifetime.</span>
<span class="sd"> Ideally we would want to release the shared objects once the stage is</span>
<span class="sd"> complete, but we don&#39;t have information about that. As such, we work around</span>
<span class="sd"> this limitation as follows: when an object is first initialised, we create and</span>
<span class="sd"> maintain an explicit reference to it. This means that it will always have one</span>
<span class="sd"> reference to it from within _SharedMap.</span>
<span class="sd"> When acquire is called for a *different* object, we delete explicit references</span>
<span class="sd"> to *all other objects*. This means that if there are no external references to</span>
<span class="sd"> these objects, they will be garbage collected.</span>
<span class="sd"> This has the following implications:</span>
<span class="sd"> * A shared object won&#39;t be GC&#39;ed if there isn&#39;t another acquire called for</span>
<span class="sd"> a different shared object. This is okay for our use-cases. This means</span>
<span class="sd"> that the shared object will be kept alive for all stages fused with the</span>
<span class="sd"> stage that works with the shared object. However, all these stages would</span>
<span class="sd"> be allocated the same memory anyway, even if the shared object</span>
<span class="sd"> were released after the stage that uses it was done with it.</span>
<span class="sd"> * Each stage can only use exactly one Shared token, otherwise only one</span>
<span class="sd"> Shared token, *NOT NECESSARILY THE LATEST*, will be &quot;kept-alive&quot; (using</span>
<span class="sd"> multiple shared tokens per-stage won&#39;t affect correctness, but will have</span>
<span class="sd"> no performance benefit either)</span>
<span class="sd"> * If there are two different stages using separate Shared tokens, but which</span>
<span class="sd"> get fused together, only one Shared token will be &quot;kept-alive&quot;. This</span>
<span class="sd"> effectively means that the Shared tokens do nothing: since S2 displaces S1,</span>
<span class="sd"> and after S2 executes a new thread is created starting with S1 again, which</span>
<span class="sd"> displaces S2.</span>
<span class="sd"> Related issues:</span>
<span class="sd"> BEAM-562 - DoFn reuse</span>
<span class="sd"> &quot;&quot;&quot;</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="c1"># Lock that protects cache_map</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
<span class="c1"># Dictionary of references to shared control blocks</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_cache_map</span> <span class="o">=</span> <span class="p">{}</span>
<span class="c1"># Tuple of (key, obj), where obj is an object we explicitly hold a reference</span>
<span class="c1"># to keep it alive</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_keepalive</span> <span class="o">=</span> <span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">make_key</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c1"># type: (...) -&gt; Text</span>
<span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid1</span><span class="p">())</span>
<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">key</span><span class="p">,</span> <span class="c1"># type: Text</span>
<span class="n">constructor_fn</span><span class="p">,</span> <span class="c1"># type: Callable[[], Any]</span>
<span class="n">tag</span><span class="o">=</span><span class="kc">None</span> <span class="c1"># type: Any</span>
<span class="p">):</span>
<span class="c1"># type: (...) -&gt; Any</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Acquire a reference to a Shared object.</span>
<span class="sd"> Args:</span>
<span class="sd"> key: the key to the shared object</span>
<span class="sd"> constructor_fn: function that initialises / constructs the object if not</span>
<span class="sd"> present in the cache. This function should take no arguments. It should</span>
<span class="sd"> return an initialised object, or None if the object could not be</span>
<span class="sd"> initialised / constructed.</span>
<span class="sd"> tag: an optional indentifier to store with the cached object. If</span>
<span class="sd"> subsequent calls to acquire use different tags, the object will be</span>
<span class="sd"> reloaded rather than returned from cache.</span>
<span class="sd"> Returns:</span>
<span class="sd"> A reference to the initialised object, either from the cache, or</span>
<span class="sd"> newly-constructed.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="p">:</span>
<span class="n">control_block</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cache_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
<span class="k">if</span> <span class="n">control_block</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">control_block</span> <span class="o">=</span> <span class="n">_SharedControlBlock</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_cache_map</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">control_block</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">control_block</span><span class="o">.</span><span class="n">acquire</span><span class="p">(</span><span class="n">constructor_fn</span><span class="p">,</span> <span class="n">tag</span><span class="p">)</span>
<span class="c1"># Because we release the lock in between, if we acquire multiple Shareds</span>
<span class="c1"># in a short time, there&#39;s no guarantee as to which one will be kept alive.</span>
<span class="k">with</span> <span class="bp">self</span><span class="o">.</span><span class="n">_lock</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_keepalive</span> <span class="o">=</span> <span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
<span class="k">return</span> <span class="n">result</span>
<span class="c1"># Instance of the shared map to be used with Shared objects.</span>
<span class="n">_shared_map</span> <span class="o">=</span> <span class="n">_SharedMap</span><span class="p">()</span>
<div class="viewcode-block" id="Shared"><a class="viewcode-back" href="../../../apache_beam.utils.shared.html#apache_beam.utils.shared.Shared">[docs]</a><span class="k">class</span> <span class="nc">Shared</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Handle for managing shared per-process objects.</span>
<span class="sd"> Each instance of a Shared object represents a distinct handle to a distinct</span>
<span class="sd"> object. Example usage is described in the file comment of shared.py.</span>
<span class="sd"> This object has the following limitations:</span>
<span class="sd"> * A shared object won&#39;t be GC&#39;ed if there isn&#39;t another acquire called for</span>
<span class="sd"> a different shared object.</span>
<span class="sd"> * Each stage can only use exactly one Shared token, otherwise only one</span>
<span class="sd"> Shared token, *NOT NECESSARILY THE LATEST*, will be &quot;kept-alive&quot;.</span>
<span class="sd"> * If there are two different stages using separate Shared tokens, but which</span>
<span class="sd"> get fused together, only one Shared token will be &quot;kept-alive&quot;.</span>
<span class="sd"> (See documentation of _SharedMap for details.)</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># TODO(altay): Consider allowing users to also pass in a key (GUID)</span>
<span class="c1"># for more easily sharing of identifiable expensive objects. User would be</span>
<span class="c1"># responsible for handling collisions.</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="bp">self</span><span class="o">.</span><span class="n">_key</span> <span class="o">=</span> <span class="n">_shared_map</span><span class="o">.</span><span class="n">make_key</span><span class="p">()</span>
<div class="viewcode-block" id="Shared.acquire"><a class="viewcode-back" href="../../../apache_beam.utils.shared.html#apache_beam.utils.shared.Shared.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">constructor_fn</span><span class="p">,</span> <span class="c1"># type: Callable[[], Any]</span>
<span class="n">tag</span><span class="o">=</span><span class="kc">None</span> <span class="c1"># type: Any</span>
<span class="p">):</span>
<span class="c1"># type: (...) -&gt; Any</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Acquire a reference to the object associated with this Shared handle.</span>
<span class="sd"> Args:</span>
<span class="sd"> constructor_fn: function that initialises / constructs the object if not</span>
<span class="sd"> present in the cache. This function should take no arguments. It should</span>
<span class="sd"> return an initialised object, or None if the object could not be</span>
<span class="sd"> initialised / constructed.</span>
<span class="sd"> tag: an optional indentifier to store with the cached object. If</span>
<span class="sd"> subsequent calls to acquire use different tags, the object will be</span>
<span class="sd"> reloaded rather than returned from cache.</span>
<span class="sd"> Returns:</span>
<span class="sd"> A reference to an initialised object, either from the cache, or</span>
<span class="sd"> newly-constructed.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="n">_shared_map</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">_key</span><span class="p">,</span> <span class="n">constructor_fn</span><span class="p">,</span> <span class="n">tag</span><span class="p">)</span></div></div>
</pre></div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
&copy; 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>