【问题标题】:App Engine development server fails to execute queued task - with no stack trace whatsoeverApp Engine 开发服务器无法执行排队任务 - 没有任何堆栈跟踪
【发布时间】:2016-10-05 17:26:58
【问题描述】:

我目前正在尝试使用 Flask 框架在 App Engine 中对任务进行排队,但遇到了一些困难。我运行我的代码,当我在 localhost:8000/taskqueue 检查管理服务器时,似乎任务已正确排队。但是,控制台反复打印以下错误:

WARNING  2016-10-05 17:08:09,560 taskqueue_stub.py:1981] Task task1 failed to execute. This task will retry in 0.100 seconds

此外,似乎没有执行所需的代码。

我的问题是,为什么我的代码不起作用?对于这个非常广泛的问题,我深表歉意,但是没有堆栈跟踪可以指导我解决更具体的问题。然而,我已经简化了我的代码以使我的错误可重现。下面的代码应将短语“示例任务”打印到控制台 5 次。但是,这不会发生。

#main.py

from google.appengine.api.taskqueue import taskqueue
from flask import Flask, Response

app = Flask(__name__)

@app.route("/get")
def get():
    for i in range(5):
        # attempt to execute the desired function 5 times
        # the message "sample task" should be printed to the console five times
        task = taskqueue.add(
            queue_name='my-queue',
            url='/sample_task',
        )
        message += 'Task {} enqueued, ETA {}.<br>'.format(task.name, task.eta)

    response = Response(message)
    return response

@app.route("/sample_task")
def sample_task():
    message = "sample task"
    print (message)
    return Response(message)


if __name__ == "__main__":
    app.run()

app.yaml

# app.yaml

runtime: python27
api_version: 1
threadsafe: true

# [START handlers]
handlers:
- url: /sample_task
  script: main.app
  login: admin

- url: /get
  script: main.app
  login: admin

queue.yaml

# queue.yaml

queue:
- name: my-queue
  rate: 1/s
  bucket_size: 40
  max_concurrent_requests: 1

【问题讨论】:

  • @Anthon:感谢您花时间解决我的问题。有反引号是因为我的印象是它们是在 StackOverflow 中呈现代码块所必需的。看来我弄错了。感谢您指出。代码块现已更正。此外,我实际上已经找到了解决问题的方法。看起来我只需要在路由器的输入参数中添加methods= 'POST'。但是,仍然感谢您检查我的代码。

标签: python google-app-engine flask google-app-engine-python


【解决方案1】:

我在这里找到了答案:https://stackoverflow.com/a/13552794

显然,我所要做的就是将 POST 方法添加到调用队列的任何处理程序。

所以

@app.route("/sample_task")
def sample_task():
...

应该是:

@app.route("/sample_task", methods=['POST'])
def sample_task():
...

【讨论】:

    猜你喜欢
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多