12
votes

I have started using Jinja2 as my templating engine on Google App Engine (in Python).

My question is this: Will bytecode caching work in production? It is working very well on the development server, but I read somewhere that bytecode caching depends on the marshal module, which is not supported in App Engine. This answer to a different question provides a possible solution by changing marshal to use pickle methods. Has anyone tried this?

In general, is there anything else that I should take into consideration when using Jinja2 with App Engine?

Any help would be greatly appreciated!

Thanks.

3
It turns out that pickle can't be used for code objects (that's why Jinja2 uses marshal to persist its compiled template bytecode). marshal is available in the local development environment, but it is an empty module in the deployed GAE environment (see code.google.com/appengine/docs/python/runtime.html#Pure_Python). - Cameron

3 Answers

12
votes

Rodrigo Moraes created some special loaders for Jinja2 under GAE, see here. It's not bytecode caching but it precompiles all templates to Python so you avoid the Jinja2 parsing overhead.

Note that (from this GAE page):

compiled application code is cached for rapid responses to web requests

9
votes

Jinja2 is now included in GAE. Apparently you need to migrate your app to Python 2.7.

In app.yaml add

libraries:
- name: jinja2
  version: "2.6"

Here is the source of this information: http://blog.notdot.net/2011/11/Migrating-to-Python-2-7-part-2-Webapp-and-templates

1
votes

According to the What's New in Python 2.7 docs, with Python 2.7 you can also upload .pyc files.

Can upload .pyc files, but not in combination with .py files. However, you can upload .zip files containing .py or .pyc files (or a combination).