小编Jon*_*Rom的帖子

更好的解决方案,用于Python Threading.Event半忙等待

我正在使用非常标准的Threading.Event:主线程在一个循环中运行:

event.wait(60)
Run Code Online (Sandbox Code Playgroud)

其他阻止请求,直到回复可用,然后启动:

event.set()
Run Code Online (Sandbox Code Playgroud)

我希望主线程选择40秒,但事实并非如此.从Python 2.7源代码Lib/threading.py:

# Balancing act:  We can't afford a pure busy loop, so we
# have to sleep; but if we sleep the whole timeout time,
# we'll be unresponsive.  The scheme here sleeps very
# little at first, longer as time goes on, but never longer
# than 20 times per second (or the timeout time remaining).
endtime = _time() + timeout
delay = 0.0005 # 500 us -> initial delay of 1 ms
while …
Run Code Online (Sandbox Code Playgroud)

python events multithreading busy-loop

7
推荐指数
1
解决办法
1595
查看次数

标签 统计

busy-loop ×1

events ×1

multithreading ×1

python ×1