gpt4 book ai didi

python 多处理队列 get([block[, timeout]]),阻塞队列中的项目 还是阻塞队列?

转载 作者:行者123 更新时间:2023-12-01 02:48:44 34 4
gpt4 key购买 nike

get([block[, timeout]])
Remove and return an item from the queue. If optional args block is True (the default) and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Queue.Empty exception if no item was available within that time. Otherwise (block is False), return an item if one is immediately available, else raise the Queue.Empty exception (timeout is ignored in that case).

如上面的文档。我编写了一个程序,只有一个生产者进程和六个消费者。进程之间共享一个队列。

  • 生产者使用方法:put_nowait()
    1 进程 * 6000 项/秒
  • 当消费者使用get_nowait()时,消费者的get_nowait非常慢。
    6 进程 * (0 ~ 500) 项/秒。
  • 当消费者使用 get(True,1) 时,程序运行良好。
    6 进程 * (1000 ~ 1600) 项/秒。

在我看来。 get_nowait() 应该比 get(block) 更快get() 方法的 block 是如何工作的,谁能给我一些建议。

最佳答案

getget_nowait 之间的性能没有差异。改变的只是它的行为。文档对此进行了详细描述。

get_nowait相当于调用:

Queue.get(False)

这意味着调用者在等待新数据可用时不会被阻塞。它不会让调用者变得“更快”。

如果您想趁机消费某些可用的东西,并在其他情况下执行其他操作,则可以使用 get_nowait 方法。

您体验到的实际速度差异可能是由于与阻塞相比,非阻塞 get 对其内部 Lock 进行了更多的访问。这会增加其成本。

您可以看到get_nowait的实际实现 here .

关于python 多处理队列 get([block[, timeout]]),阻塞队列中的项目 还是阻塞队列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45023958/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com