2010-12-04 61 views
2

我正在使用隊列api,並遇到錯誤使我的程序崩潰。Erlang隊列問題

首先我取從字典它返回這個在打印輸出

隊列取出的隊列是[{[],[]}]

這是正常的?隊列是否正確創建?

然後,無論是當我嘗試添加到隊列或獲得其長度,我得到一個錯誤badargs兩個。

TorrentDownloadQueue = dict:fetch(torrentDownloadQueue, State), 
io:format("The fetched queue is ~p~n", [dict:fetch(torrentDownloadQueue, State)]), 
% Add the item to the front of the queue (main torrent upload queue) 
TorrentDownloadQueue2 = queue:in_r(Time, TorrentDownloadQueue), 
% Get the lenght of the downloadQueue 
TorrentDownloadQueueLength = queue:len(TorrentDownloadQueue2), 

當嘗試插入值10中的誤差是

**原因終止== ** {badarg,[{隊列,IN_R,[10,[{[] ,[]}]]},{ ph_speed_calculator,handle_cast,2},{ gen_server,HANDLE_MSG,5},{ proc_lib,init_p_do_apply,3}]} **退出異常:在功能隊列badarg :IN_R/2 呼叫隊列:in_r(10,[{[],[]}]) 來自ph_speed_calculator的呼叫:ha ndle_cast/2 從gen_server電話:HANDLE_MSG/5 呼叫從proc_lib:init_p_do_apply/3 13>

這是IN_R的錯誤,但我得到了LEN呼叫badargs錯誤了。

我稱呼這些的方式有什麼問題,或者是初始隊列不正確? 我創建隊列如下並將其添加到字典中

TorrentDownloadQueue = queue:new(), 
TorrentUploadQueue = queue:new(), 
D4 = dict:store(torrentDownloadQueue, [TorrentDownloadQueue], D3), 
D5 = dict:store(torrentUploadQueue, [TorrentUploadQueue], D4), 

我不知道我做錯了。

回答

6

你有什麼([{[],[]}])是一個列表中的隊列。標準隊列看起來像{list(), list()}。顯然,正因爲如此,隨後每次對隊列的調用都會失敗。

我的猜測是你要麼排隊錯,要麼以錯誤的方式提取。

+0

非常感謝。它現在看起來很清楚我做錯了什麼,我認爲字典的價值必須在[]內。我不知道我爲什麼這樣做。我一遍又一遍地檢查了我的代碼,忽略了這一點。我無法感謝你 – jarryd 2010-12-04 15:12:17

2

首先,你得到的是一個「badarg」錯誤。從二郎隊列的文檔閱讀:

所有功能會失敗,原因badarg 如果論點是錯誤的類型, 例如隊列參數不 隊列,索引不是整數,列表 參數不是列表。不正確的 列表導致內部崩潰。隊列超出範圍的索引 也會導致 故障,原因badarg。

在你的情況下,你傳遞給隊列:in_r不是隊列,而是包含隊列的列表。

你將含有排隊時你做的詞典列表:

D4 = dict:store(torrentDownloadQueue, [TorrentDownloadQueue], D3), 
D5 = dict:store(torrentUploadQueue, [TorrentUploadQueue], D4), 

你應該這樣做,相反,簡單地說:

D4 = dict:store(torrentDownloadQueue, TorrentDownloadQueue, D3), 
D5 = dict:store(torrentUploadQueue, TorrentUploadQueue, D4), 
+0

非常感謝你對這兩個問題的幫助。所以我正確地傳遞了狀態。我不知道爲什麼我認爲價值必須是[價值]。 :) – jarryd 2010-12-04 15:19:17