问题
I have an eunit test that generates a unique node name and starts distribution:
{A,B,C} = now(),
Nodename = list_to_atom(lists:flatten(io_lib:format(
"test-~b-~b-~b@localhost", [A, B, C]))),
{ok, _} = net_kernel:start([Nodename, shortnames]),
This works fine as long as a distributed Erlang node has been running on the machine at some previous time, and thus epmd is still running, but on the build server I can't assume that's the case.
I solved the problem by adding this to my test:
_ = os:cmd("epmd -daemon"),
but it feels like a hack. Is there a better/nicer way to ensure that epmd is started before running net_kernel:start
?
回答1:
No, you cannot ensure EPMD is started in a cleaner way.
TL;DR
EPMD is an external program, implemented in C. Whilst net_kernel:start/1
takes care of creating the net_sup supervisor, it does not actually trigger the EPMD daemon, which has to be started explicitely. I had a look at how EPMD is started when the -sname
option is specified in the erl
command and - surprise, surprise - I discovered that the epmd program is started via a system() C call.
来源:https://stackoverflow.com/questions/18240319/ensure-epmd-started