更新:这个问题基于Queue.get()实际行为的错误心智模型,这是由一些略微模糊的文档引起的,但主要是由timedelta.total_seconds()的错误的手动实现引起的.当我试图证明原始答案不正确时,我发现了这个错误.现在timedelta.total_seconds()由
Python提供(自2.7起),我将继续使用它.
对困惑感到抱歉.
这不是“我的代码为什么不运行?”问题,但“这个设计决定背后的动机是什么?”
从2.3开始,Python的队列模块包含一个带有get方法的Queue类,它接受一个timeout参数.这是手册中的部分:
Queue.get([block[, timeout]])
Remove and return an item from the queue. If optional args block is true 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 Empty exception if no item was available within that time. […]
(强调我的)
请注意,即使尚未达到超时,它也可能引发Empty异常.事实上,我在Ubuntu(但不是Windows)上看到了这种行为.它只是提前退出,它对我的代码产生了轻微的影响 – 我可以编写代码.
大多数阻塞超时都会占用最小超时,这在非实时操作系统(包括Windows和Linux)上是有意义的.无法保证操作系统在任何给定的截止日期之前将上下文切换到您的进程或线程.
但是,这个最大超时.任何人都可以解释这个设计决定可能有意义吗?