【发布时间】:2019-10-23 10:06:14
【问题描述】:
更新:此错误的修复已提交,并将在 Python 3.10 中首次亮相,预计将于 2021 年 10 月发布。有关详细信息,请参阅bug report。
time.perf_counter() 的文档表明它是系统范围的
时间。perf_counter() → 浮动
返回性能计数器的值(以秒为单位),即 a 具有最高可用分辨率的时钟以测量短持续时间。 它确实包括睡眠期间经过的时间,并且是系统范围的。这 返回值的参考点未定义,因此只有 连续调用结果之间的差异是有效的。
我将系统范围解释为包括跨进程的一致性是否不正确?
如下所示,在 Linux 上似乎是一致的,但在 Windows 上却不是。此外,Python 3.6 的 Windows 行为与 3.7 显着不同。
如果有人能指出涵盖此行为的文档或错误报告,我将不胜感激。
测试用例
import concurrent.futures
import time
def worker():
return time.perf_counter()
if __name__ == '__main__':
pool = concurrent.futures.ProcessPoolExecutor()
futures = []
for i in range(3):
print('Submitting worker {:d} at time.perf_counter() == {:.3f}'.format(i, time.perf_counter()))
futures.append(pool.submit(worker))
time.sleep(1)
for i, f in enumerate(futures):
print('Worker {:d} started at time.perf_counter() == {:.3f}'.format(i, f.result()))
Windows 7 上的结果
C:\...>Python36\python.exe -VV
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)]
C:\...>Python36\python.exe perf_counter_across_processes.py
Submitting worker 0 at time.perf_counter() == 0.000
Submitting worker 1 at time.perf_counter() == 1.169
Submitting worker 2 at time.perf_counter() == 2.170
Worker 0 started at time.perf_counter() == 0.000
Worker 1 started at time.perf_counter() == 0.533
Worker 2 started at time.perf_counter() == 0.000
C:\...>Python37\python.exe -VV
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
C:\...>Python37\python.exe perf_counter_across_processes.py
Submitting worker 0 at time.perf_counter() == 0.376
Submitting worker 1 at time.perf_counter() == 1.527
Submitting worker 2 at time.perf_counter() == 2.529
Worker 0 started at time.perf_counter() == 0.380
Worker 1 started at time.perf_counter() == 0.956
Worker 2 started at time.perf_counter() == 1.963
为简洁起见,我在 Windows 上省略了进一步的结果,但在 Windows 8.1 上观察到了相同的行为。此外,Python 3.6.7 的行为与 3.6.8 相同,而 Python 3.7.1 的行为与 3.7.3 相同。
在 Ubuntu 18.04.1 LTS 上的结果
$ python3 -VV
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0]
$ python3 perf_counter_across_processes.py
Submitting worker 0 at time.perf_counter() == 2075.896
Submitting worker 1 at time.perf_counter() == 2076.900
Submitting worker 2 at time.perf_counter() == 2077.903
Worker 0 started at time.perf_counter() == 2075.900
Worker 1 started at time.perf_counter() == 2076.902
Worker 2 started at time.perf_counter() == 2077.905
$ python3.7 -VV
Python 3.7.1 (default, Oct 22 2018, 11:21:55)
[GCC 8.2.0]
$ python3.7 perf_counter_across_processes.py
Submitting worker 0 at time.perf_counter() == 1692.514
Submitting worker 1 at time.perf_counter() == 1693.518
Submitting worker 2 at time.perf_counter() == 1694.520
Worker 0 started at time.perf_counter() == 1692.517
Worker 1 started at time.perf_counter() == 1693.519
Worker 2 started at time.perf_counter() == 1694.522
【问题讨论】:
-
我已经为此问题提交了错误报告:bugs.python.org/issue37205
标签: python-3.x windows time multiprocessing