Skip to content

Instantly share code, notes, and snippets.

@dergraf
Created October 10, 2014 13:24
Show Gist options
  • Save dergraf/b3aa9d495110ff30a010 to your computer and use it in GitHub Desktop.
Save dergraf/b3aa9d495110ff30a010 to your computer and use it in GitHub Desktop.
set up a function call in its own process
make_request(Fun, Args) when is_function(Fun), is_list(Args) ->
Ref = make_ref(),
Caller = {self(), Ref},
ReqF = fun() ->
exit({Ref, apply(Fun, [Caller|Args])})
end,
try spawn_monitor(ReqF) of
{_, MRef} ->
receive
Ref ->
erlang:demonitor(MRef, [flush]),
ok;
{'DOWN', MRef, process, Reason} ->
{error, Reason}
end
catch
error: system_limit = E ->
{error, E}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment