python - 延迟任务创建无法访问某些 python 模块的新实例

标签 python google-app-engine google-cloud-endpoints

我正在使用具有自动缩放、端点 API 和 deferred.defer() 任务的最新版本的 GAE。

问题是自添加 API 以来,有些实例会自动启动,但总是会引发永久性任务失败:

Permanent failure attempting to execute task
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 310, in post
    self.run_from_request()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 305, in run_from_request
    run(self.request.body)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 145, in run
    raise PermanentTaskFailure(e)
PermanentTaskFailure: No module named app.Report

永久任务失败对于单个实例来说是唯一的,其中该实例上的每个延迟任务都会失败。这些延迟的任务都抛出相同的错误,即使这些任务没有使用 Api.py 模块。在其他情况下,相同的延迟任务如果没有被路由到失败的实例,它们将运行得很好。

app.yaml 处理程序如下所示:

handlers:
# Api Handler
- url: /_ah/api/.*
  script: main.api
- url: /_ah/spi/.*
  script: main.api
# All other traffic
- url: .*
  script: main.app

builtins:
- deferred: on

main.py 看起来像:

import Api, endpoints, webapp2

api = endpoints.api_server([Api.AppApi])

app = webapp2.WSGIApplication( 
    [(misc routes)]
,debug=True)

Api.py 看起来像:

import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from google.appengine.ext import deferred
from app.Report import ETLScheduler

@endpoints.api(...)
class AppApi(remote.Service):
    @endpoints.method(...)
    def reportExtract(self, request):
        deferred.defer(
            ETLScheduler,
            params
        )

我没有做任何路径修改,所以我很好奇为什么新实例无法找到 API 的 python 模块,即使延迟任务在另一个使用其他函数的模块中。为什么它只会为那个实例抛出这些错误?

编辑:

所以在查看了其他一些 SO 问题之后,我尝试在 appengine_config.py 中进行路径修改。我将所有文件夹移动到 lib 目录,并将其添加到配置文件中:

import os,sys
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib'))

现在我在失败实例上得到的错误是:

Permanent failure attempting to execute task
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 310, in post
    self.run_from_request()
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 305, in run_from_request
    run(self.request.body)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/deferred/deferred.py", line 145, in run
    raise PermanentTaskFailure(e)
PermanentTaskFailure: cannot import name ETLScheduler

所以它似乎正在寻找模块,但和以前一样,实例上的延迟任务都不能导入该方法。

最佳答案

所以我找到了一种方法让它工作,但我不确定它为什么会工作。

通过导入整个模块而不是模块中的方法,为延迟任务启动的新实例不再抛出 PermanentTaskFailure: cannot import name ETLScheduler 错误。

我尝试导入整个模块而不是方法,因此 Api.py 看起来像这样:

import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
from google.appengine.ext import deferred

# Import the module instead of the method
#from app.Report import ETLScheduler
import app.Report

@endpoints.api(...)
class AppApi(remote.Service):
    @endpoints.method(...)
    def reportExtract(self, request):
        deferred.defer(
            app.Report.ETLScheduler,
            params
        )

现在我不再获得抛出 PermanentTaskFailure: cannot import name ETLScheduler 的实例。可能是在 main.py 中导入 Api.py 的循环依赖(我不确定)但至少它现在可以工作了。

关于python - 延迟任务创建无法访问某些 python 模块的新实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27789874/

相关文章:

python - 来自 pandas DataFrame 的热图 - 二维数组

python - 如何检查嵌套容器是否发生变异?

python - 在嵌套循环中使用 multiprocessor.Pool 的正确方法

python - 如何在私有(private)发布服务器上发布?

google-app-engine - Google App Engine 可选斜杠重定向

python - ansible正则表达式替换所有匹配的string1,排除string2

android-studio - Android Studio 3 - 如何添加云端点模块

python - 如何返回动态 json 作为来自谷歌云端点 python 的响应

android - appengine 连接的 android 项目在到达时死机

google-app-engine - 为什么 WebApp2 auth.get_user_by_session() 更改 token ?