dat*_*yss 5 python optimization google-app-engine
我对蟒蛇世界比较陌生,但这似乎很直接.
谷歌对我大吼大叫这个代码需要优化:
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))
Run Code Online (Sandbox Code Playgroud)
仪表板告诉我这是使用了大量的CPU.
我应该在哪里寻找改进措施?
这里的主要开销是多个单独放入数据存储区.如果可以的话,将链接存储为单个实体,正如安德烈建议的那样.您始终可以将链接拆分为数组并将其存储在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)
Run Code Online (Sandbox Code Playgroud)
它会将数据存储区往返减少到一个,并且这是真正杀死你的高CPU上限的往返行程.
| 归档时间: |
|
| 查看次数: |
1087 次 |
| 最近记录: |