Erlang队列问题

标签 erlang queue

我正在使用队列 api 并且遇到了导致我的程序崩溃的错误。

首先我从字典中获取队列并在打印输出中返回它

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

这正常吗?队列创建是否正确?

然后,当我尝试添加到队列或获取其长度时,我都会在两者上收到错误 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 时,错误是

** 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>

这是 in_r 的错误,但我也收到 len 调用的 badargs 错误。

我调用这些的方式有什么问题,或者初始队列不正确? 我按如下方式创建队列并将其添加到字典中

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

我不知道我做错了什么。

最佳答案

您拥有的 ([{[],[]}]) 是列表中的一个队列。标准队列看起来像 {list(), list()}。显然,正因为如此,后续对队列的每次调用都会失败。

我的猜测是,您要么携带错误的队列,要么以错误的方式提取它。

关于Erlang队列问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4353461/

相关文章:

erlang - 使用 RabbitMQ 延迟消息交换插件时出现“函数未导出”错误

android - 关于 Service 和 IntentService 之间特定 "difference"的问题

java - 如果我在遍历队列时将对象添加到队列中,是否会收到 ConcurrentModificationException 异常?

synchronization - 如何将这段代码转换为非阻塞和无锁?

algorithm - 将事件队列用于类似 cron 的目的是不是一个坏主意?

python - 如何以编程方式检查 Google Appengine 中任务队列的状态?

erlang - 无法在 Erlang 中运行简单的应用程序

Erlang 二进制泄漏?

eclipse - 二郎erlIDE : what are the -compile options supported?

scala - Actor 模型是否仅限于特定语言?