2011-02-10 156 views
2

所以,這兒是個模塊,我寫的一部分:模塊功能

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

回答

2

您的循環函數可能會被調用,但上面顯示的代碼只是該函數的一個子句,只有在使用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