【发布时间】: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