Modules function in Erlang
So, this here is part of 开发者_如何学运维a module I'm writing:
request(Pid, ListOfDocuments) when is_pid(Pid), is_list(ListOfDocuments) ->
io:format("We get here~n"),
case whereis(jdw_api) of
undefined -> [no, api];
ApiPid when is_pid(ApiPid) ->
io:format("... and here~n"),
% (1) Won't work: spawn(?MODULE, loop, [Pid, ListOfDocuments])
% (2) Won't work, either: ?MODULE:loop(Pid, ListOfDocuments)
loop(Pid, ListOfDocuments) % (3) and neither does this...
end.
... and so is this:
loop(Pid, Docs) when is_list(Docs), length(Docs) > 0 ->
H = hd(Docs),
T = tl(Docs),
io:format("... but not here...~w~n", H),
case ?MODE of
sync ->
Ref = make_ref(),
jdw_api ! {self(), Ref, doc, H},
Ans = loop_sync(Ref, [], []),
Pid ! Ans,
loop(Pid, T);
async -> {error, 'async mode not implemented yet', ?FILE, ?LINE};
_ -> {'?MODE must be either async or sync'}
end;
loop(Pid, Docs) -> io:format("Done with document list").
... but for some reason, the "loop" function never gets called. Neither of the three different ways makes the magic happen... Any pointers?
Your loop function might get called, but the code you show above is only one clause of that function, and it will only run if called with a Pid and a non-empty list of documents. The other problem is your buggy call to io:format("... but not here...~w~n", H)
which should be io:format("... but not here...~w~n", [H])
. That call might be crashing the loop code. Without more source to work from and example arguments to request/2
it's hard to tell.
精彩评论