4

我想使用 line_profiler 分析代码,其中有线程。使用 native/naive 代码实现,以下代码不会捕获线程在do_other-stuff

from line_profiler import LineProfiler
import random, threading, time

def do_other_stuff(numbers):
    time.sleep(1)
    s = sum(numbers)

def do_stuff(numbers):
    my_thread = threading.Thread(target=do_other_stuff,args=[numbers])
    my_thread.start()

numbers = [random.randint(1,100) for i in range(1000)]
lp = LineProfiler()
lp.add_function(do_other_stuff)
lp_wrapper = lp(do_stuff)
lp_wrapper(numbers)
lp.print_stats()

结果是

Timer unit: 1e-07 s

Total time: 0 s
File: <ipython-input-5-304263bfd927>
Function: do_other_stuff at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def do_other_stuff(numbers):
     5                                               time.sleep(1)
     6                                               s = sum(numbers)

Total time: 0.0008575 s
File: <ipython-input-5-304263bfd927>
Function: do_stuff at line 8

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     8                                           def do_stuff(numbers):
     9         1        578.0    578.0      6.7      my_thread = threading.Thread(target=do_other_stuff,args=[numbers])
    10         1       7997.0   7997.0     93.3      my_thread.start()

知道如何在线路配置文件中包含线程吗?

4

0 回答 0