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").
... 但由于某种原因,“循环”函数永远不会被调用。这三种不同的方式都无法让魔法发生……有什么指示吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的循环函数可能会被调用,但上面显示的代码只是该函数的一个子句,并且只有在使用 Pid 和非空文档列表调用时才会运行。另一个问题是您对 io:format("... 但不在这里...~w~n", H) 的错误调用应该是 io:format(".. .但不是在这里...~w~n", [H])。该调用可能会使循环代码崩溃。如果没有更多的源代码和
request/2
的示例参数,很难说清楚。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 beio:format("... but not here...~w~n", [H])
. That call might be crashing the loop code. Without more source to work from and example arguments torequest/2
it's hard to tell.