Google App Engine で Python 2.7 が使えるようになっていた。
違いは What’s New in Python 2.7 – Google App Engine — Google Developers にまとまっているんだけれど、手を動かさないと理解できない。
そこで Introduction – Google App Engine — Google Developers に従って Hello World し直してみた。使っている PC は Windows7 64bit。
手順:
- Python 2.7 Release から Windows x86 MSI Installer をダウンロードしてインストール。
- Downloads – Google App Engine — Google Developers から GoogleAppEngine-1.6.5.msi をダウンロードしてインストール。「Python2.5が見つかった」と表示されるけど気にしない。
- Google App Engine Launcherを起動して、Python2.7を利用するように修正(Edit > Preferences > Python Path)。
あとはチュートリアルに従ってソースコードを書いて、Google App Engine Launcher にプロジェクトとして追加し、Runすればよい。
古い helloworld.py (Python2.5) – Using the webapp Framework – Google App Engine — Google Developers
from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app class MainPage(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Hello, webapp World!') application = webapp.WSGIApplication( [('/', MainPage)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main()
新しい helloworld.py (Python2.7)
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, webapp World!')
app = webapp2.WSGIApplication([('/', MainPage)],
debug=True)
ということで、webapp ではなく webapp2 が使われるにように。main() の記述もやめてスッキリ。
古い app.yaml (Python2.5) – Hello, World! – Google App Engine — Google Developers
application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py
新しい app.yaml (Python2.7)
application: helloworld version: 1 runtime: python27 api_version: 1 threadsafe: true handlers: - url: /.* script: helloworld.app
違いは3点。runtimeでPython2.7を使うことを宣言する。threadsafeなWebアプリにすれば、複数のリクエストを並列実行して効率的にさばける。
main()を呼び出さないようにしたので、helloworld.pyで定義したhelloworldモジュールのappオブジェクトへ処理させることをここで明示するようになった。