Python 如何将参数传递到任务队列?

Python 如何将参数传递到任务队列?,python,google-app-engine,flask,task-queue,flask-restful,Python,Google App Engine,Flask,Task Queue,Flask Restful,我将在Flask/Google应用程序引擎中实现推送任务队列。 本质上,我希望发布到API并在任务队列中执行底层工作 初始入口点是一个RESTAPI(flask_restful) 辅助对象定义为url.py中的视图: app.add_url_rule('/worker', 'worker', view_func=csrf_protect.exempt(TaskView.as_view('taskView'))) TaskView是: from flask.gl

我将在Flask/Google应用程序引擎中实现推送任务队列。 本质上,我希望发布到API并在任务队列中执行底层工作

初始入口点是一个RESTAPI(flask_restful)

辅助对象定义为url.py中的视图:

app.add_url_rule('/worker', 'worker',
                 view_func=csrf_protect.exempt(TaskView.as_view('taskView')))
TaskView是:

from flask.globals import request

class TaskView(MethodView):
    def post(self):
        user = request.json['user']
        return "OK"
奇怪的是,当我在
TaskView
中调试时,请求对象中没有任何我发送给
/worker
的用户对象的跟踪。然而,我在那里发现
记录了上次调用的
对象

请问我缺少什么?

试试:

taskqueue.add(url='/worker',params={'user':user},method=“POST”)

user=request.form.get('user')


正如marcadian指出的,taskqueue默认使用
POST
,因此您可能需要
request.form
来访问POST vars。

很抱歉线程复活,但是如果您想直接使用json,您可以执行以下操作,例如:

taskstop = taskqueue.add(url='/stop_std_watcher',
                                    target='worker',
                                    payload=json.dumps({'watcherjson': client.client_watcher}),
                                    method='POST',
                                    headers={'Content-Type': 'application/json'})
重要的位是添加
标题
有效负载
kwargs

在接收端,您得到:

@app.route('/stop_std_watcher', methods=['POST'])
def stop_std_watcher():
    # curl -i -H "Content-Type: application/json" -X POST -d @stop.json http://localhost:8081/stop_std_watcher
    watcherjson = request.json['watcherjson']

我在注释中添加了curl,如果您想手动测试post路由,它会很有用。

默认情况下,它只在调试器和请求中使用POSI checked。表单
是一个空字典。我遇到了一个有趣的案例。在Eclipse/debug中,我得到了如上所述的行为。但是,如果部署正确,我实际上会在
request.form
中看到
user
。真奇怪。你确定生产和开发中都存在同一个用户吗?从
FTRecordsAPI
开始,尝试在每个步骤记录
user
。我假设您的taskqueue正在开发中?您确定它将进入taskqueue.add行吗?任务是否已创建?也许你的url应该是/worker而不是/worker/?是的,我在
TaskView
->
user=request.json['user']
中的断点实际上断开了。它肯定在那里。
@app.route('/stop_std_watcher', methods=['POST'])
def stop_std_watcher():
    # curl -i -H "Content-Type: application/json" -X POST -d @stop.json http://localhost:8081/stop_std_watcher
    watcherjson = request.json['watcherjson']