How can I optimize this Google App Engine code?
google-app-engine, optimization, python
Solution
The main overhead here is the multiple individual puts to the datastore. If you can, store the links as a single entity, as Andre suggests. You can always split the links into an array and store it in a ListProperty.
If you do need an entity for each link, try this:
# 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)
It will reduce the datastore roundtrips to one, and it's the roundtrips that are really killing your high CPU cap.
Problem
I'm relatively new to the Python world, but this seems very straight forward. Google is yelling at me that this code needs to be optimized: ``` 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)) ``` The dashboard is telling me that this is using a ton of CPU. Where should I look for improvements?