模块功能

问题描述:

所以,这儿是个模块,我写的一部分:模块功能

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

...但由于某种原因,“循环”功能永远不会被调用。三种不同的方式都不会使魔法发生......任何指针?

+0

你是什么意思“永远不会叫”?什么都没有发生,或者进程崩溃,或者什么? @archaelus提到的`H`和`[H]`错误会导致进程崩溃,如果它没有链接到任何东西,那么崩溃将是沉默的。 – rvirding 2011-02-10 23:21:05

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

+0

其他子句位于代码块的正下方。这只是一个格式问题。 (没有警卫,它只能做一个io:格式。)很好地捕捉到“H”和“H”问题。 – Tadmas 2011-02-10 22:52:16