duanqiao1926 2013-06-18 07:44
浏览 164
已采纳

在channel.Consume()上使用channel.Get()有缺点吗?

I'm using streadway's amqp library to connect with a rabbitmq server. The library provides a channel.Consume() function which returns a "<- chan Delivery". It also provides a channel.Get() function which returns a "Delivery" among other things.

I've to implement a pop() functionality, and I'm using channel.Get(). However, the documentation says:

"In almost all cases, using Channel.Consume will be preferred."

Does the preferred here means recommended? Are there any disadvantages of using channel.Get() over channel.Consume()? If yes, how do I use channel.Consume() to implement a Pop() function?

  • 写回答

2条回答 默认 最新

  • douzhen1234 2013-06-18 09:06
    关注

    As far as I can tell from the docs, yes, "preferred" does mean "recommended".

    It seems that channel.Get() doesn't provide as many features as channel.Consume(), as well as being more readily usable in concurrent code due to it's returning a chan of Delivery, as opposed to each individual Delivery separately.

    The extra features mentioned are exclusive, noLocal and noWait, as well as an optional Table of args "that have specific semantics for the queue or server."

    To implement a Pop() function using channel.Consume() you could, to link to some code fragments from the amqp example consumer, create a channel using the Consume() function, create a function to handle the chan of Delivery which will actually implement your Pop() functionality, then fire off the handle() func in a goroutine.

    The key to this is that the channel (in the linked example) will block on sending if nothing is receiving. In the example, the handle() func uses range to process the entire channel until it's empty. Your Pop() functionality may be better served by a function that just receives the last value from the chan and returns it. Every time it's run it will return the latest Delivery.

    EDIT: Example function to receive the latest value from the channel and do stuff with it (This may not work for your use case, it may be more useful if the function sent the Delivery on another chan to another function to be processed. Also, I haven't tested the code below, it may be full of errors)

    func handle(deliveries <-chan amqp.Delivery, done chan error) {
        select {
        case d = <-deliveries:
            // Do stuff with the delivery
            // Send any errors down the done chan. for example:
            // done <- err
        default:
            done <- nil
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析