使用cProfile与asyncio代码的正确方法是什么?

时间:2019-02-15 23:15:56

标签: python profiling python-asyncio epoll cprofile

我正在尝试确定如何正确地将cProfile和pstats与asyncio代码一起使用。我正在通过运行cProfile.run('loop.run_until_complete(main())', 'profile.stats')来分析脚本。

使用pstatsSortKeys.TIME进行排序后,我得到以下输出:

In [9]: sorted_stats.print_stats()
Fri Feb 15 14:40:18 2019    profile.txt

         67411199 function calls (67230882 primitive calls) in 305.899 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     3617  273.162    0.076  273.162    0.076 {method 'poll' of 'select.epoll' objects}
14094092/14093931    1.527    0.000    1.904    0.000 {built-in method builtins.isinstance}
    44901    1.166    0.000    1.166    0.000 {built-in method posix.stat}
   784052    1.028    0.000    1.479    0.000 /usr/lib/python3/dist-packages/soupsieve/util.py:41(lower)
    32070    0.887    0.000    1.565    0.000 /usr/lib/python3/dist-packages/bs4/element.py:620(<genexpr>)
  4554545    0.729    0.000    0.756    0.000 /usr/lib/python3/dist-packages/bs4/element.py:1331(descendants)
  1026560    0.726    0.000    3.358    0.000 /usr/lib/python3/dist-packages/bs4/element.py:1502(search)
   448385    0.716    0.000    1.411    0.000 /usr/lib/python3/dist-packages/bs4/element.py:1528(_matches)
      104    0.689    0.007    4.771    0.046 /usr/lib/python3/dist-packages/bs4/builder/_lxml.py:283(feed)
   255143    0.655    0.000    1.575    0.000 /usr/lib/python3/dist-packages/bs4/__init__.py:391(endData)
   137377    0.520    0.000    3.692    0.000 /usr/lib/python3/dist-packages/html5lib/_tokenizer.py:49(__iter__)
     8574    0.510    0.000    2.392    0.000 /usr/lib/python3.7/traceback.py:318(extract)
   167700    0.494    0.000    1.061    0.000 /usr/lib/python3/dist-packages/bleach/html5lib_shim.py:199(charsUntil)
    53607    0.487    0.000    0.589    0.000 /usr/lib/python3/dist-packages/bs4/builder/__init__.py:158(_replace_cdata_list_attribute_values)
   299357    0.485    0.000    0.485    0.000 /usr/lib/python3/dist-packages/bs4/element.py:258(setup)
    22253    0.467    0.000    5.928    0.000 /usr/lib/python3/dist-packages/bs4/element.py:592(_find_all)
   448385    0.437    0.000    2.225    0.000 /usr/lib/python3/dist-packages/bs4/element.py:1461(search_tag)
    83147    0.424    0.000    2.365    0.000 /usr/lib/python3/dist-packages/bs4/builder/_lxml.py:176(start)
  1293015    0.418    0.000    0.556    0.000 /usr/lib/python3/dist-packages/soupsieve/css_match.py:94(is_tag)
  4824419    0.409    0.000    0.409    0.000 {method 'append' of 'list' objects}
    73068    0.404    0.000    0.634    0.000 /home/jess/.local/lib/python3.7/site-packages/rfc3986/normalizers.py:127(encode_component)
   868978    0.403    0.000    0.773    0.000 /usr/lib/python3/dist-packages/bleach/html5lib_shim.py:192(char)
   454702    0.385    0.000    0.385    0.000 {method 'match' of 're.Pattern' objects}
  2326981    0.374    0.000    0.415    0.000 {built-in method builtins.hasattr}
       52    0.334    0.006    5.732    0.110 /usr/lib/python3/dist-packages/html5lib/html5parser.py:196(mainLoop)


   ...       ...       ...      ...      ...          ... 

如您所见,几乎所有执行时间都显示为{method 'poll' of 'select.epoll' objects}

我对cProfileasyncio没有足够的经验,无法知道如何解释此结果。我无法判断统计信息是否显示我的程序在poll()上花费了大部分执行时间,因为(a)cProfile无法与asyncio配合使用,并且没有提供正确的统计信息,(b)这是正常现象,我应该忽略第一行,或者(c)这表明程序中存在某种错误(由于我尚未确定的原因,现在确实确实很慢)...

异步进程在poll()中花费这么多执行时间是正常的吗?是否可以使用cProfile或其他方法更好地配置异步代码,从而为我提供更准确的结果?还是这表明我的代码中可能存在某种错误导致此错误?


注意:就上下文而言,我分析的代码基本上是获取50页,处理HTML,然后进行一些文本/语言处理。 fetch()部分可以在poll()中解释大部分时间。但这是通过快速的Internet连接(> 5MB /秒)实现的,因此我认为它不应该花费那百分比的时间来获取几十个text/html页面。似乎正在发生其他情况,或者统计信息不正确...

0 个答案:

没有答案