このブログはURLが変更になりました

新しいブログはこちら→ https://matsuu.hatenablog.com/

Python2.6環境でGoogle App Engineのdev_appserver.pyがエラーになる問題

Python2.6環境でGoogle App Engine SDKのdev_appserver.pyを動かすと、以下のようなエラーが出る場合がある。
SDKのバージョンは1.2.4。

Traceback (most recent call last):
  File "/usr/lib64/python2.6/wsgiref/handlers.py", line 93, in run
    self.result = application(self.environ, self.start_response)
  File "/tmp/google_appengine/google/appengine/ext/webapp/__init__.py", line 523, in __call__
    handler.handle_exception(e, self.__debug)
  File "/tmp/google_appengine/google/appengine/ext/webapp/__init__.py", line 388, in handle_exception
    logging.exception(exception)
  File "/usr/lib64/python2.6/logging/__init__.py", line 1433, in exception
    error(*((msg,)+args), **{'exc_info': 1})
  File "/usr/lib64/python2.6/logging/__init__.py", line 1426, in error
    root.error(*((msg,)+args), **kwargs)
  File "/usr/lib64/python2.6/logging/__init__.py", line 1056, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib64/python2.6/logging/__init__.py", line 1142, in _log
    record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
  File "/usr/lib64/python2.6/logging/__init__.py", line 1117, in makeRecord
    rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
  File "/usr/lib64/python2.6/logging/__init__.py", line 272, in __init__
    from multiprocessing import current_process
  File "/tmp/google_appengine/google/appengine/tools/dev_appserver.py", line 1158, in Decorate
    return func(self, *args, **kwargs)
  File "/tmp/google_appengine/google/appengine/tools/dev_appserver.py", line 1806, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/tmp/google_appengine/google/appengine/tools/dev_appserver.py", line 1158, in Decorate
    return func(self, *args, **kwargs)
  File "/tmp/google_appengine/google/appengine/tools/dev_appserver.py", line 1708, in FindAndLoadModule
    description)
  File "/tmp/google_appengine/google/appengine/tools/dev_appserver.py", line 1158, in Decorate
    return func(self, *args, **kwargs)
  File "/tmp/google_appengine/google/appengine/tools/dev_appserver.py", line 1659, in LoadModuleRestricted
    description)
  File "/usr/lib64/python2.6/multiprocessing/__init__.py", line 83, in <module>
    import _multiprocessing
ImportError: No module named _multiprocessing

で、Issue 1159: Logging with dev_appserver.py using Python 2.6 failsに、dev_appserver.pyの先頭に以下を追加する対策が提案されているが、これは問題を強引に避けているだけだと思う。多分。

IMO it's not the right approach to make dev_appserver.py work with Python 2.6 as it's
not the Python version your application is using in production environment. BTW, I
think it's much easier to add

import logging
logging.logMultiprocessing = 0

somewhere at the top of dev_appserver.py than monkeypatching the multiprocessing module.

But it would be nice if dev_appserver.py would print a warning if it's started with a
Python version other than 2.5...

sandbox環境で_multiprocessing.soが読み込めないからじゃないかと推測しながらソースを調べたところ、google/appengine/tools/dev_appserver.py内の_WHITE_LIST_C_MODULESに_multiprocessingを追加すれば良さそうだと判明。

ということで、_multiprocessingを追加するパッチを作成した

--- google_appengine/google/appengine/tools/dev_appserver.py.orig	2009-08-07 05:18:49.000000000 +0900
+++ google_appengine/google/appengine/tools/dev_appserver.py	2009-08-18 22:00:05.000000000 +0900
@@ -1266,6 +1266,7 @@
       '_lsprof',
       '_md5',
       '_multibytecodec',
+      '_multiprocessing',
       '_random',
       '_sha',
       '_sha256',

一応これで動作するようになった。が、これが正しい対策である保証はない。後ほどIssue 1159にも報告する。