GAE Python dev服务器在升级到2.7后间歇性崩溃 [英] GAE Python dev server crashes intermittently after upgrade to 2.7

查看:94
本文介绍了GAE Python dev服务器在升级到2.7后间歇性崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的GAE Python应用程序升级到了Python 2.7。从那以后,我定期在dev服务器上得到以下错误,dev服务器提供了一个空白页面:

  Traceback最近调用最后一次):
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py中的第168行,处理
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime /wsgi.py,第206行,位于_LoadHandler
handler = __import __(path [0])
[...]
文件/ Users / joneill / OpenSTV / OpenSTV / trunk / OpaVote-HR / main.py,第2行,在< module>
导入视图
[...]
文件/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/views.py,第3行,位于< module>
from pytz.gae import pytz
[...]
文件/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/pytz/__init__.py,第34行,在< module>
from pkg_resources import resource_stream
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py,第662行,装饰
返回func(self,* args,** kwargs)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/ appengine / tools / dev_appserver_import_hook.py,第1818行,在load_module
中返回self.FindAndLoadModule(submodule,fullname,search_path)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default。 bundle / Contents / Resources / google_appengine / google / appengine / tools / dev_appserver_import_hook.py,第662行,装饰
返回func(self,* args,** kwargs)
文件/ Applications / GoogleAppEngineLauncher 。应用程序/内容/资源/ GoogleAppEngine-default.bundle /内容/资源/ google_ap pengine / google / appengine / tools / dev_appserver_import_hook.py,第1690行,在FindAndLoadModule
描述中)
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources /google_appengine/google/appengine/tools/dev_appserver_import_hook.py,第662行,装饰
返回func(self,* args,** kwargs)
文件/Applications/GoogleAppEngineLauncher.app/Contents/在LoadModuleRestricted
返回source_file.load_module(submodule_fullname)
文件/ Applications / Resources / GoogleAppEngine-default.bundle / Contents / Resources / google_appengine / google / appengine / tools / dev_appserver_import_hook.py GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/dist/py_zipimport.py,第246行,在load_module
submodname,is_package,fullpath,source = self中。 _get_source(fullmodname)
文件/ Applications / GoogleAppEngineLau nget.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/dist/py_zipimport.py,第207行,位于_get_source
source = self.zipfile.read(relpath。替换(os.sep,'/'))
读取
中的文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py,第867行返回self.open(name,r,pwd).read()
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py,第882行,打开
zef_file = open(self.filename,'rb')
文件/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine /tools/dev_appserver_import_hook.py,第578行,在__init__
引发IOError(errno.EACCES,'文件不可访问',文件名)
IOError:[Errno 13] file not accessible:'/ Library / Frameworks / Python.framework / Versions / 2.7 / lib / python2.7 / site-packages / setuptools-0.6c11-py2.7.egg'
INFO 2012-01-21 20:50:44,222 dev_appserver.py:2832]POST / manage HTTP / 1.1500 -

有些注释:


  • 生产服务器上不会发生这种情况。 li>在开发服务器上,我的应用程序将运行几分钟,然后发生此错误。

  • 如果我在开发服务器上停止并重新启动应用程序,它将再次运行几分钟。

  • 我正在使用最新版本的 gae -pytz ,你可以看到它在导入时失败。

  • 我删除的内容与您在结尾处看到的内容类似。 / li>
  • 我不知道为什么在最后调用setuptools。

  • 我在Lion上使用Mac。



我可以使用开发服务器,但每隔几分钟停止并重新启动确实很烦人。任何想法如何解决这个问题?

解决方案

堆栈跟踪的实际问题是,您的代码尝试导入安装工具网站包,dev服务器不会这样做。



'/ Library / Frameworks / Python.framework / Versions / 2.7 / lib / python2.7 / site- packages / setuptools-0.6c11-py2.7.egg'



您需要在您的应用程序代码库中包含setuptools。事实上,它有时会起作用,这表明你通过各种模块编码路径各不相同,也许(取决于你在开发中测试的)不同的导入顺序意味着设置工具已经导入别的地方,或者只在你的代码中的某些点需要



查看导入pytz的堆栈跟踪的第4行,下一行是从pkg_resources导入resource_stream那是什么引发了其余的导入问题。我在我的项目的根目录中使用了伪截断的pkg_resources,这并不会导致尝试从设置工具导入东西。您可以在调试导入模式下运行开发服务器,这将告诉您更多的信息。



这是一个伪造的pkg_resources。

 包资源API 
--------------------

资源是一个包含在一个包中的逻辑文件,或者是一个逻辑
子目录。包资源API期望资源名
的路径部分用``/``分开,*不包括*本地
路径分隔符是。不要使用os.path操作来操作资源
名称被传递到API。

包资源API被设计用来处理普通的文件系统包,
.egg文件以及解压后的.egg文件,还可以通过
.zip文件以及支持get_data()$ b $的自定义PEP 302加载器b方法。


import sys,os,zipimport,time,re,imp,new

尝试:
frozenset
除了NameError:
from sets import ImmutableSet as frozenset

from os import utime#,rename,unlink#捕获它们绕过沙箱
从os导入打开为os_open

可能还有其他的/更好的方式来做这件事,但它对我很有用。



哦,我还建议你使用 http://code.google.com/p/gae-pytz/ 代替pytz。



干杯


I recently upgraded my GAE Python app to Python 2.7. Since then, I periodically get the following error with the dev server and the dev server serves up a blank page:

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 168, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 206, in _LoadHandler
    handler = __import__(path[0])
  [...]
  File "/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/main.py", line 2, in <module>
    import views
  [...]
  File "/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/views.py", line 3, in <module>
    from pytz.gae import pytz
  [...]
  File "/Users/joneill/OpenSTV/OpenSTV/trunk/OpaVote-HR/pytz/__init__.py", line 34, in <module>
    from pkg_resources import resource_stream
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 662, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1818, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 662, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1690, in FindAndLoadModule
    description)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 662, in Decorate
    return func(self, *args, **kwargs)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 1615, in LoadModuleRestricted
    return source_file.load_module(submodule_fullname)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/dist/py_zipimport.py", line 246, in load_module
    submodname, is_package, fullpath, source = self._get_source(fullmodname)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/dist/py_zipimport.py", line 207, in _get_source
    source = self.zipfile.read(relpath.replace(os.sep, '/'))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 867, in read
    return self.open(name, "r", pwd).read()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 882, in open
    zef_file = open(self.filename, 'rb')
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 578, in __init__
    raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg'
INFO     2012-01-21 20:50:44,222 dev_appserver.py:2832] "POST /manage HTTP/1.1" 500 -

Some notes:

  • This doesn't happen on the production server.
  • On the dev server, my app will work for a few minutes and then this error happens.
  • If I stop and restart my app on the dev server, it will work again for a few minutes.
  • I am using the latest version of gae-pytz and you can see that it fails in an import there.
  • The [...] that I removed are similar to the stuff you see near the end.
  • I don't know why setuptools is being invoked at the end.
  • I'm using a Mac with Lion.

I can use the dev server, but it is really annoying to stop and restart every few minutes. Any ideas how to fix this?

解决方案

The actual problem from the stack trace, is your code is trying to import setup tools from site-packages, which the dev server won't do.

'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg'

You will need to include setuptools in you application code base. The fact that it works sometimes, suggests that you code paths through various modules vary, and maybe (depending on what your tesing in dev) different import orders mean setup tools has been imported somewhere else, or is only required at certain points in your code.

Have a look at line 4th line of the stack trace where pytz is imported, the next line is from pkg_resources import resource_stream thats whats triggering the rest of the import issue. I use a fake truncated pkg_resources at the root of my project, that doesn't end up trying to import stuff from setup tools. You can run the dev server in debug import mode which will tell you a lot more

Here is a fake pkg_resources.

"""Package resource API
--------------------

A resource is a logical file contained within a package, or a logical
subdirectory thereof.  The package resource API expects resource names
to have their path parts separated with ``/``, *not* whatever the local
path separator is.  Do not use os.path operations to manipulate resource
names being passed into the API.

The package resource API is designed to work with normal filesystem packages,
.egg files, and unpacked .egg files.  It can also work in a limited way with
.zip files and with custom PEP 302 loaders that support the ``get_data()``
method.
"""

import sys, os, zipimport, time, re, imp, new

try:
    frozenset
except NameError:
   from sets import ImmutableSet as frozenset

from os import utime   #, rename, unlink    # capture these to bypass sandboxing
from os import open as os_open

There are probably other/better ways of doing this, but it works for me.

Oh, I would also suggest you use http://code.google.com/p/gae-pytz/ instead of pytz.

Cheers

这篇关于GAE Python dev服务器在升级到2.7后间歇性崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆