Erlang queue problems

I am using the queue api and have encountered to errors which crash my program.

First off I fetch the queue from the dictionary which returns this in a printout

The fetched queue is [{[],[]}]

Is this normal? Is the queue created correctly?

Then either when I try add to the queue or get its length I get an error badargs on both.

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),

When I try to insert the value 10 the error is

** Reason for termination == ** {badarg,[{queue,in_r,[10,[{[],[]}]]}, {ph_speed_calculator,handle_cast,2}, {gen_server,handle_msg,5}, {proc_lib,init_p_do_apply,3}]} ** exception exit: badarg in function queue:in_r/2 called as queue:in_r(10,[{[],[]}]) in call from ph_speed_calculator:handle_cast/2 in call from gen_server:handle_msg/5 in call from proc_lib:init_p_do_apply/3 13>

This is the error for the in_r, but I get a badargs error for the len call too.

What is wrong with the way I call these or is the inital Queue incorrect? I create the queue as follows and add it to the dictionary

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

I have no idea what I am doing wrong.


What you have ( [{[],[]}] ) is a queue in a list. A standard queue looks like {list(), list()} . Obviously, because of this, every subsequent call to queues will fail.

My guess is that you either carry your queue wrong or extract it in the wrong manner.


First of all, what you've got is a "badarg" error. Reading from the documentation for Erlang queues:

All functions fail with reason badarg if arguments are of wrong type, for example queue arguments are not queues, indexes are not integers, list arguments are not lists. Improper lists cause internal crashes. An index out of range for a queue also causes a failure with reason badarg.

In your case, what you pass to the queue:in_r is not a queue, but a list containing a queue.

You're adding a list containing a queue to the dictionary when you do:

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

You should do, instead, simply:

D4 = dict:store(torrentDownloadQueue, TorrentDownloadQueue, D3),
D5 = dict:store(torrentUploadQueue, TorrentUploadQueue, D4),
链接地址: http://www.djcxy.com/p/38182.html

上一篇: 服务器成为瓶颈?

下一篇: Erlang队列问题