python concurrent queue_python – 使用concurrent.futures一次消耗许多出列的消息

我正在使用来自RabbitMQ频道的消息,我希望我一次可以消耗n个元素.我想我可以使用ProcessPoolExecutor(或ThreadPoolExecutor).

我只是想知道是否可以知道池中是否有免费执行程序.

这就是我想写的:

executor = futures.ProcessPoolExecutor(max_workers=5)

running = []

def consume(message):

print "actually consuming a single message"

def on_message(channel,method_frame,header_frame,message):

# this method is called once per incoming message

future = executor.submit(consume,message)

block_until_a_free_worker(executor,future)

def block_until_a_free_worker(executor,future):

running.append(future) # this grows forever!

futures.wait(running,timeout=5,return_when=futures.FIRST_COMPLETED)

[...]

channel.basic_consume(on_message,'my_queue')

channel.start_consuming()

我需要编写函数block_until_a_free_worker.

此方法应该能够检查是否所有正在运行的工作程序都在使用中.

在替代方案中,我可以使用任何阻塞executor.submit选项(如果可用).

我尝试了一种不同的方法,并在完成后改变期货清单.

我试图从列表中明确添加和删除期货,然后像这样等待:

futures.wait(running,return_when=futures.FIRST_COMPLETED)

这似乎不是解决方案.

我可以设置future.add_done_callback,并可能计算正在运行的实例…

任何提示或想法?

谢谢.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值