DeadlineExceededError:使用Django

时间:2018-04-01 15:47:58

标签: django google-app-engine django-rest-framework mysql-python

我正在尝试在Google App Engine上托管API(使用Django休息框架)。在根域(https://xxxxxx-xxxxxx-xxxxxx.appspot.com/)没有问题,但是当我去端点/产品/我遇到服务器500错误并检查日志时我看到DeadlineExceededError: The overall deadline for responding to the HTTP request was exceeded错误。

值得注意的是,/产品在我的本地机器上按预期工作 - 并在一秒钟内加载。

跟踪堆栈如下所示:

Traceback (most recent call last):
File "/base/alloc/tmpfs/dynamic_runtimes/python27/6baee03590d046f1_unzipped/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/wsgi.py", line 157, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/base.py", line 124, in get_response
response = self._middleware_chain(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/utils/deprecation.py", line 140, in __call__
response = self.get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/viewsets.py", line 87, in view
return self.dispatch(request, *args, **kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/views.py", line 471, in dispatch
response = handler(request, *args, **kwargs)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/mixins.py", line 48, in list
return Response(serializer.data)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/serializers.py", line 701, in data
ret = super(ListSerializer, self).data
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/serializers.py", line 240, in data
self._data = self.to_representation(self.instance)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/rest_framework/serializers.py", line 619, in to_representation
self.child.to_representation(item) for item in iterable
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/models/sql/compiler.py", line 887, in execute_sql
cursor = self.connection.cursor()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/base/data/home/apps/g~xxxxxx-xxxxxx-xxxxxx/20180401t160704.408718556380642667/lib/django/db/utils.py", line 86, in __exit__
dj_exc_value = dj_exc_type(*exc_value.args)
DeadlineExceededError: The overall deadline for responding to the HTTP request was exceeded.

看起来它连接到数据库时遇到问题所以我将包含我的settings.py文件:

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so connect to Google Cloud SQL using
    # the unix socket at /cloudsql/<your-cloudsql-connection string>
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': 'xx.xxx.xxx.xx',
            'NAME': 'products',
            'USER': 'xxx',
            'PASSWORD': 'xxx',
            'PORT': '3306'
        }
    }

和urls.py:

from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from app import views

router = routers.DefaultRouter()
router.register(r'products', views.ProductViewSet)

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
    url(r'^$', views.api_root),
    url(r'^products/', views.ProductViewSet)
]

和我的app.yaml文件:

runtime: python27
api_version: 1
threadsafe: true

handlers:

- url: /static
  static_dir: static

- url: /.*
  script: xxxxxxx_project.wsgi.application

libraries:

- name: django
  version: "latest"

- name: MySQLdb
  version: "latest"

env_variables:
    DJANGO_SETTINGS_MODULE: 'xxxxxxx_project.settings'
    CLOUDSQL_CONNECTION_NAME: xxxxxx-xxxxxx-xxxxxx:europe-west1:xxxxxxx-xxx
    CLOUDSQL_USER: xxx
    CLOUDSQL_PASSWORD: xxx

任何帮助都会非常感激!

1 个答案:

答案 0 :(得分:1)

修改settings.py文件以使其通过套接字连接到Cloud SQL可能是值得的。示例代码可以在 repeatedly 找到。

如果这没有帮助 - DeadlineExceededError有多个原因;有关可能原因的更详细信息可以在 here 中找到,您可能会觉得这些信息很有用。