Erlang 中的模块函数

发布于 2024-10-16 18:48:31 字数 1100 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-10-23 18:48:31

您的循环函数可能会被调用,但上面显示的代码只是该函数的一个子句,并且只有在使用 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文