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

标签 python google-app-engine profile python-2.7

如何在 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/

相关文章:

mysql - x-devapi 无法连接到 Google 应用程序引擎中的数据库

iphone - 在 Debug模式下找不到此可执行文件的有效配置文件

maven-2 - 可用的 Maven 配置文件

python - 在 Python 中,我的函数不返回值

python - 如何将包含相同参数子集且值相同的 2 个函数合并为 1 个函数?

google-app-engine - Golang GAE 将图像 Url 保存到 Blobstore

python - 使用 Google App Engine Blobstore 下载的文件名

java - 如何让maven中的资源目录正常工作?

Python异常文件排序

python - 图像python opencv中的圆形轮廓检测