模块在Erlang中运行

时间:2011-02-10 22:32:30

标签: function module erlang

所以,这是我正在撰写的模块的一部分:

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.

......所以是这样的:

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").

...但由于某种原因,“循环”函数永远不会被调用。三种不同的方式都没有让魔术发生......任何指针?

1 个答案:

答案 0 :(得分:2)

您的循环函数可能会被调用,但您上面显示的代码只是该函数的一个子句,并且只有在使用Pid和非空文档列表调用时才会运行。另一个问题是您对io:format("... but not here...~w~n", H)的错误调用应该是io:format("... but not here...~w~n", [H])。该调用可能会使循环代码崩溃。没有更多的工作来源和request/2的示例参数,很难说。