In Elixir how can you spawn a long running process (tail-called) that sleeps without hanging IEX?
Example:
def longrunner() do
IO.puts "do something..."
:timer.sleep(10000) // :timer.sleep blocks IEX repl
longrunner()
end
myrunner = spawn(longrunner)
the problem is how you are calling it.
spawn(longrunner)
tries to run and evaluate the longrunner
function (due to optional parentheses) before it calls spawn
. That is why it is hanging.
Try:
myrunner = spawn(fn() -> longrunner() end)
1 Like
longrunner
is not a variable/binding, it is a function, thus it is actually doing this:
myrunner = spawn(longrunner())
Of which would never return so spawn never even gets called. You probably want to pass the function rather than call the function:
myrunner = spawn(&longrunner/0)
1 Like