title: HashDict and OTP GenServer context within Elixir
body: <p>I am having trouble using the <code>HashDict</code> function within OTP. I would like to use one <code>GenServer</code> process to <code>put</code> and a different one to <code>fetch</code>. When I try and implement this, I can put and fetch items from the <code>HashDict</code> when calling from the same GenServer; it works perfectly (<code>MyServerA</code> in the example below). But when I use one <code>GenServer</code> to <code>put</code> and a different one to <code>fetch</code>, the fetch implementation does not work. Why is this? Presumably it's because I need to pass the <code>HashDict</code> data structure around between the three different processes?  </p>
  
  <p>Code example below:</p>
  
  
  
  <p>I use a simple call to send some state to <code>MyServerB</code>:</p>
  
  <pre class="lang-py prettyprint-override"><code>    MyServerA.add_update(state)
  </code></pre>
  
  <p>For <code>MyServerB</code> I have implemented the <code>HashDict</code> as follows:</p>
  
  <pre class="lang-py prettyprint-override"><code>defmodule MyServerB do
    use GenServer
  
    def start_link do
      GenServer.start_link(__MODULE__,[], name: __MODULE__)
    end
  
    def init([]) do
      #Initialise HashDict to store state
      d = HashDict.new
      {:ok, d}
    end
  
    #Client API
    def add_update(update) do
      GenServer.cast __MODULE__, {:add, update}
    end
  
    def get_state(window) do
      GenServer.call __MODULE__, {:get, key}
    end
  
    # Server APIs 
    def handle_cast({:add, update}, dict) do
      %{key: key} = update
      dict = HashDict.put(dict, key, some_Value)
    {:noreply, dict}
    end
  
    def handle_call({:get, some_key}, _from, dict) do
      value = HashDict.fetch!(dict, some_key)
      {:reply, value, dict}
    end
  end
  </code></pre>
  
  <p>So if from another process I use <code>MyServerB.get_state(dict,some_key)</code>, I don't seem to be able to return the contents of the <code>HashDict</code>...</p>
  
  <p>UPDATE:</p>
  
  <p>So if I use ETS I have something like this:</p>
  
  <pre class="lang-py prettyprint-override"><code>def init do
     ets = :ets.new(:my_table,[:ordered_set, :named_table])
     {:ok, ets}
  end
  
  def handle_cast({:add, update}, state) do
     update = :ets.insert(:my_table, {key, value})
     {:noreply, ups}
  end 
  
  def handle_call({:get, some_key}, _from, state) do
     sum = :ets.foldl(fn({{key},{value}}, acc) 
          when key == some_Key -&gt; value + acc 
                        (_, acc) -&gt;
                           acc 
                         end, 0, :my_table) 
     {:reply, sum, state}
  end
  </code></pre>
  
  <p>So again, the <code>cast</code> works - when I check with <code>observer</code> I can see the its filling up with my key value pairs. However, when I try my <code>call</code> it returns nothing again. So I'm wondering if I'm handling the state incorrectly?? Any help, gratefully received??</p>
  
  <p>Thanks</p>