Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何优化此Google应用程序引擎代码?_Python_Google App Engine_Optimization - Fatal编程技术网

Python 如何优化此Google应用程序引擎代码?

Python 如何优化此Google应用程序引擎代码?,python,google-app-engine,optimization,Python,Google App Engine,Optimization,我对Python世界还比较陌生,但这似乎很简单 谷歌对我大喊大叫,这段代码需要优化: class AddLinks(webapp.RequestHandler): def post(self): # Hash the textarea input to generate pseudo-unique value hash = md5.new(self.request.get('links')).hexdigest() # Se

我对Python世界还比较陌生,但这似乎很简单

谷歌对我大喊大叫,这段代码需要优化:

class AddLinks(webapp.RequestHandler):
     def post(self):
          # Hash the textarea input to generate pseudo-unique value
          hash = md5.new(self.request.get('links')).hexdigest()

          # Seperate the input by line
          allLinks = self.request.get('links').splitlines()

          # For each line in the input, add to the database
          for x in allLinks:
               newGroup = LinkGrouping()
               newGroup.reference = hash
               newGroup.link = x
               newGroup.put()

          # testing vs live
          #baseURL = 'http://localhost:8080'
          baseURL = 'http://linkabyss.appspot.com'

          # Build template parameters
          template_values = {
               'all_links': allLinks,
               'base_url': baseURL,
               'reference': hash,
          }

          # Output the template
          path = os.path.join(os.path.dirname(__file__), 'addLinks.html')
          self.response.out.write(template.render(path, template_values))   
仪表板告诉我这是在使用大量的CPU


我应该在哪里寻求改进?

对我来说似乎很难

我认为有一件事可能会有一点改进。 您的呼叫“self.request.get('links')”两次

因此,加入:

unsplitlinks = self.request.get('links')
而引用“未链接”可能会有所帮助

除此之外,循环是我看到的唯一一个优化目标。
是否可以准备数据,然后立即将其添加到数据库中,而不是按链接添加数据库?(我假设.put()命令将链接添加到数据库中)

调用该链接的频率是多少?这看起来没那么糟。。。尤其是在删除重复请求后。

只需将完整的
self.request.get('links')
存储在数据库的文本字段中,就可以显著减少应用程序与数据库之间的交互

  • 每个
    post(self)
  • 散列不会被存储n次(对于每个链接,这毫无意义,而且实际上是在浪费空间)

当有人实际调用页面时,您可以保存对文本字段的解析…

这里的主要开销是对数据存储的多个单独输入。如果可以,按照Andre的建议,将链接存储为单个实体。您始终可以将链接拆分为一个数组,并将其存储在ListProperty中

如果每个链接都需要实体,请尝试以下操作:

# For each line in the input, add to the database
groups = []
for x in allLinks:
     newGroup = LinkGrouping()
     newGroup.reference = hash
     newGroup.link = x
     groups.append(newGroup)
db.put(groups)

它会将数据存储的往返次数减少到一次,而正是这些往返次数真正消耗了您的高CPU容量。

我可以查询ListProperty吗

差不多

SELECT * FROM LinkGrouping WHERE links.contains('http://www.google.com')
我有未来的计划,我需要的功能


我肯定会实现单个db.put()以减少使用。

否/您不能使用类似“links.contains(“”)”的内容
GQL不支持此功能

是的,ListProperties有一个很酷的功能。如果执行LinkGrouping.gql(“WHERE links=:1“,”),它将返回其列表中包含“”的所有组。