gpt4 book ai didi

python - 如何分析 Google App Engine python27 运行时(不是 python)

转载 作者:太空狗 更新时间:2023-10-29 21:15:40 26 4
gpt4 key购买 nike

如何在 Google App Engine 运行时 python27分析 python 代码?

在运行时 python 它是由这段代码完成的 - python runtime :

from google.appengine.ext import webapp

class PageHandler(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, WebApp World!')

def real_main():
application = webapp.WSGIApplication([('/', PageHandler)], debug=True)
run_wsgi_app(application)

def profile_main():
# This is the main function for profiling
# We've renamed our original main() above to real_main()
import cProfile, pstats, StringIO
prof = cProfile.Profile()
prof = prof.runctx('real_main()', globals(), locals())
stream = StringIO.StringIO()
stats = pstats.Stats(prof, stream=stream)
stats.sort_stats('cumulative')
logging.info("Profile data:\n%s", stream.getvalue())

if __name__ == "__main__":
profile_main()

在运行时 python27 必须以不同的方式完成,因为没有主调用 - 如何做同样的事情 - 我想切换到 python27 但不是没有分析。如何在 python27 中附加分析器 - python27 runtime

import webapp2

class PageHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, WebApp World!')

app = webapp2.WSGIApplication([('/', PageHandler)])

最佳答案

您可以通过在您的 appengine_config.py 中插入使用 WSGI 中间件来分析 WSGI 应用程序:

import cProfile
import cStringIO
import logging
import pstats

def webapp_add_wsgi_middleware(app):

def profiling_wrapper(environ, start_response):
profile = cProfile.Profile()
response = profile.runcall(app, environ, start_response)
stream = cStringIO.StringIO()
stats = pstats.Stats(profile, stream=stream)
stats.sort_stats('cumulative').print_stats()
logging.info('Profile data:\n%s', stream.getvalue())
return response

return profiling_wrapper

关于python - 如何分析 Google App Engine python27 运行时(不是 python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10040783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com