Python 的 time.process_time() 是否包括子进程花费的 CPU 时间?如何包含它?

Is Python's time.process_time() including the CPU time spent by child processes? How to include it?

Python 的 time.process_time() 是否包括子进程花费的 CPU 时间?

documentation.

我不是很清楚

如果没有,我怎样才能(轻松地)包含它?

time.process_time 不包括子进程花费的时间。这在文档中非常清楚地说明:

Return the value (in fractional seconds) of the sum of the system and user CPU time of the current process.

当前进程,不是当前进程及其所有子进程。

我不知道有任何界面可以让进程跟踪其子进程的进程时间。我发现最接近的是 POSIX times 函数,它提供对 waited-for 子进程的处理时间总和的访问,但那是 platform-dependent 并且非常有限。

为了更清楚地理解,我写了一个小脚本来对比处理时间(CPU 时间)和时钟时间之间的差异。

另外,显示子进程时间不包含在CPU时间中。

import datetime
import time
from multiprocessing import Pool

from numpy import mean


def f(x):
    i = 0
    for j in range(x ** 8):
        i += j
    return i


def process_time():
    clock_t0 = time.time()
    t0 = time.process_time()
    result = f(9)
    print('Result:', result, end='\t')
    t1 = time.process_time()
    clock_t1 = time.time()
    print('CPU time: ', t1 - t0, end='\t')
    print('Clock time: ', clock_t1 - clock_t0)
    return t1 - t0


def multiprocessing_process_time():
    clock_t0 = time.time()
    t0 = time.process_time()
    with Pool(10) as pool:
        result = pool.map(f, [9])
    print('Result:', result[0], end='\t')
    t1 = time.process_time()
    clock_t1 = time.time()
    print('CPU time: ', t1 - t0, end='\t')
    print('Clock time: ', clock_t1 - clock_t0)
    return t1 - t0


if __name__ == '__main__':
    print('Processing in Parent Process\n')
    print('Mean CPU processing time:', mean([process_time() for _ in range(5)]))

    print('\nProcessing in Child Process')
    print('Mean CPU processing time:', mean([multiprocessing_process_time() for _ in range(5)]))


以上程序的输出:

Processing in Parent Process

Result: 926510072902560 CPU time:  2.620428 Clock time:  2.6484527587890625
Result: 926510072902560 CPU time:  2.6250959999999997   Clock time:  2.654899835586548
Result: 926510072902560 CPU time:  2.587252000000001    Clock time:  2.6077020168304443
Result: 926510072902560 CPU time:  2.6254989999999996   Clock time:  2.667827844619751
Result: 926510072902560 CPU time:  2.5997120000000002   Clock time:  2.6256277561187744
Mean CPU processing time: 2.6115974

Processing in Child Process
Result: 926510072902560 CPU time:  0.025433999999998846 Clock time:  2.701629877090454
Result: 926510072902560 CPU time:  0.0210480000000004   Clock time:  2.8027760982513428
Result: 926510072902560 CPU time:  0.02214200000000055  Clock time:  2.8002538681030273
Result: 926510072902560 CPU time:  0.02209799999999973  Clock time:  2.7950479984283447
Result: 926510072902560 CPU time:  0.03242999999999974  Clock time:  2.718341112136841
Mean CPU processing time: 0.0246304