如何在下的配置文件 python代码谷歌应用程序引擎运行时 python27

在运行时 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/

10-12 03:21