Suppose I have the following function:
def foo, do: IO.puts "Foobar"
I want to run it a minute after some work done in my function:
def fun(x) do
somework(x)
# Schedule foo to run after 1 minute
end
I do not want to delay the return of fun by 1 minute - I want to return immediately after somework returns (Otherwise I can just do Process.sleep). I also do not care if foo fails or not.
One thing I have done is the following:
Task.start(fn ->
Process.sleep(60_000)
&foo
end)
But if I am not mistaken, another process is started to run the task. Having a process sitting idle for a minute seems wasteful of system resources - especially if the delay is much greater than a minute and there are many tasks being created per second.
Another idea I got is to write a GenServer to handle scheduling tasks, but I avoided this because this seemed like a common pattern, and expected there to be a “preferred” solution. Also, this sort of functionality is probably better off in Task.Supervisor than a hand-written GenServer anyway.
How can I achieve this using the existing task running infrastructure?


















