Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 异步推迟google app engine中的多个任务_Python_Google App Engine_Asynchronous_App Engine Ndb - Fatal编程技术网

Python 异步推迟google app engine中的多个任务

Python 异步推迟google app engine中的多个任务,python,google-app-engine,asynchronous,app-engine-ndb,Python,Google App Engine,Asynchronous,App Engine Ndb,我有一个接受3个参数的tasklet,一个带有id和标题的字典,一个用户密钥,以及数据库中已有的记录列表。 函数defer_fetch在for循环中被调用N次 @ndb.tasklet def defer_fetch(data, user_key, already_inserted): if data['_id'] not in already_inserted: document_key = yield Document.fetch_or_create(data)

我有一个接受3个参数的tasklet,一个带有id和标题的字典,一个用户密钥,以及数据库中已有的记录列表。 函数defer_fetch在for循环中被调用N次

@ndb.tasklet
def defer_fetch(data, user_key, already_inserted):
    if data['_id'] not in already_inserted:
         document_key = yield Document.fetch_or_create(data)
         yield defer_fetch_document(user_key, document_key)
    else:
         document_key = alread_inserted[data['_id']]
    raise ndb.Return(document_key)

@ndb.tasklet
def defer_fetch_document(user_key, document_key):
    deferred.defer(process_document, user_key, document_key, _queue="process-documents")
    raise ndb.Return(True)
document.fetch\u或\u create的代码在所有defer\u fetch调用中并行执行,但调用fetch\u文档不是,如附件所示


如何使deferred\u fetch\u文档也并行运行?

据我所知,deferred与ndb.tasklets不能很好地配合使用(deferred.deferred没有收益,因此代码只是同步运行)。 相反,您应该直接使用任务队列的add_async功能

@ndb.tasklet
def defer_fetch_document(user_key, document_key):
    queue = taskqueue.Queue("process-documents")
    task = taskqueue.Task(url="<url for worker>", 
                          params={"document_key": document_key.urlsafe()})
    yield queue.add_async(task)  #this returns a rpc which you can yield on
    raise ndb.Return(True)
@ndb.tasklet
def defer_fetch_文档(用户密钥、文档密钥):
queue=taskqueue.queue(“流程文档”)
task=taskqueue.task(url=“”,
params={“document_key”:document_key.urlsafe()})
yield queue.add_async(task)#这将返回一个rpc,您可以在该rpc上进行屈服
提升ndb返回(真)
或者,生成文档键列表(从中生成任务列表),然后调用add_async with list of tasks