Dan*_*eck 9 python profiler profiling web.py
我正在调整一个大量使用生成器来生成结果的应用程序,以提供web.py Web界面.
到目前为止,我可以在函数中包含对for循环和输出生成语句的调用,并使用cProfile.run()或调用它runctx().概念:
def output():
for value in generator():
print(value)
cProfile.run('output()')
Run Code Online (Sandbox Code Playgroud)
在web.py中,我必须以下面的方式包装它,因为我想在每个迭代步骤中使用以下命令立即生成可能长时间运行的计算的输出yield:
class index:
def GET(self):
for value in generator():
yield make_pretty_html(value)
Run Code Online (Sandbox Code Playgroud)
是否有一种方法来分析所有对生成器的调用,就像在第一个例子中一样,当它像第二个例子一样使用时?
我终于找到了解决方案.通过此处返回分析值.
import cProfile
import pstats
import glob
import math
def gen():
for i in range(1, 10):
yield math.factorial(i)
class index(object):
def GET(self):
p = cProfile.Profile()
it = gen()
while True:
try:
nxt = p.runcall(next, it)
except StopIteration:
break
print nxt
p.print_stats()
index().GET()
Run Code Online (Sandbox Code Playgroud)
我还可以通过文档合并多个这样的分析结果(一旦我开始给出唯一的文件名)并将它们组合在一起存储/分析.
| 归档时间: |
|
| 查看次数: |
1660 次 |
| 最近记录: |