title: Getting user input in an elixir Task
body: <p>I'm trying to write a simple elixir program that is able to react to user input. My problem is that reading from stdio doesn't seem to work from Tasks. If my whole idea is silly please show me an example on how it's done. I can't find anything in the web</p>
  
  <p>I broke my problem down to a simple example:</p>
  
  <pre><code>t = Task.async((fn-&gt;IO.gets "what?" end))                
  %Task{owner: #PID&lt;0.65.0&gt;, pid: #PID&lt;0.80.0&gt;, ref: #Reference&lt;0.0.2.135&gt;}
  </code></pre>
  
  <p>The Task is started:</p>
  
  <pre><code>iex(4)&gt; pid=Map.get(t, :pid)
  #PID&lt;0.80.0&gt;
  iex(5)&gt; Process.alive? pid                                       
  true 
  </code></pre>
  
  <p>and alive but it doesn't print to stdio nor does it read. It's not exiting normally or with án exception. I tried IO.read/2 too.</p>
  
  <p>In my program the Task is started with Task.spawn_link/1 but the problem is the same.IO.gets/2 and code following the IO.gets/2 function is not executed.</p>
  
  <p>The Supervisor starting the Task:</p>
  
  <pre><code>defmodule Prime do
    use Application
  
      def start(_type, _args) do
         import Supervisor.Spec, warn: false
  
  children = [
    # Define workers and child supervisors to be supervised
    worker(Task, [fn-&gt;Prime.IO.communicate(nil) end], restart: :transient),
    supervisor(Prime.Test.Supervisor, [])
  ]
  
  opts = [strategy: :one_for_one, name: Prime.Supervisor]
  Supervisor.start_link(children, opts) end end
  </code></pre>
  
  <p>The tasks function:</p>
  
  <pre><code>defmodule Prime.IO do
  
  @doc """
      handles communication with the user and user demanded Actions.
  """
  
  def communicate(numTasks) do
      case(numTasks) do
  
      nil -&gt;
          {numTasks, _} =Integer.parse(IO.gets "This program searches for prime numbers per try and error.\nHow many concurrent Tasks?\n")
          Prime.IO.communicate(numTasks)
  
      x when is_number(x) -&gt;
          Prime.Test.Server.setTaskNumber(numTasks)
          Prime.IO.communicate("waiting")
  
      y when is_bitstring(y) -&gt;
          IO.puts(numTasks)
  
      _ -&gt; 
          Prime.IO.communicate(nil)
  
      end
  end
  end 
  </code></pre>