googleappengin中的延迟队列

2024-07-07 07:16:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在延迟的任务队列中执行一些任务,我需要在成功完成延迟队列中的任务后执行另一个任务。例如,我需要删除存储在给定路径上的所有文件,然后,在成功删除所有文件之后,我想重新开始创建这些文件。在

下面是我目前如何实现它的示例代码。不幸的是,它引发了以下例外:

raise PermanentTaskFailure(e) PermanentTaskFailure: 'module' object has no attribute 'DeleteTitanFiles'

在删除延迟队列中的文件之后要执行的另一个任务不一定非得是另一个任务,它可以是任何任务,甚至是简单的print语句。关键是,控件应该在执行删除后返回到下一个语句。在

from google.appengine.ext import ndb
from google3.apphosting.contrib.titan.files import files
from google.appengine.ext import deferred
import logging

TITAN_FILES_PATH = '/lovish-abc/'

BATCH_SIZE = 250

range_titan = 0

def _GetFileCount():
    return files.Files.count(TITAN_FILES_PATH, recursive=True)

file_count = _GetFileCount()

print _GetFileCount()

def CreateTitanFiles(path, start):
    logging.warning('In the CreateTitanFiles method')
    filecount = _GetFileCount()
    if filecount < 1000:
        range_titan = start + BATCH_SIZE
        for z in xrange(start, range_titan):
            titan_files = files.File(TITAN_FILES_PATH + 'file' + str(z) + '.json')
            titan_files.write(content='adasdad')
        logging.info("######sdgdgds")
        deferred.defer(
            CreateTitanFiles, TITAN_FILES_PATH, range_titan)



def DeleteTitanFiles(path):
    logging.info('In the DeleteTitanFiles method')

    filecount = _GetFileCount()

    if filecount > 0:
        titan_files = files.Files.list(
            TITAN_FILES_PATH, limit=BATCH_SIZE)
        titan_files.delete()
    else:
        CreateTitanFiles(TITAN_FILES_PATH, 0)


def CallDeleteTitanFiles(path):
    logging.warning('In the CallDeleteTitanFiles method')

    filecount = _GetFileCount()

    while filecount > 0:
        try:
            deferred.defer(DeleteTitanFiles, TITAN_FILES_PATH)
            filecount = _GetFileCount()
            logging.info('calling again')
            print filecount
        except Exception, e:
            raise e

CallDeleteTitanFiles(TITAN_FILES_PATH)

有什么建议可以达到预期的效果?在


Tags: 文件pathimport队列loggingdefrangefiles
1条回答
网友
1楼 · 发布于 2024-07-07 07:16:06

来自Limitations of the deferred library的这条注释可能与您得到该错误的原因有关:

  • You can't pass a method defined in the request handler module.

The last point above deserves special attention: passing a method defined in the request handler module - the module specified as a request handler in app.yaml - will not work. You can call deferred.defer from the request handler module, but the function you are passing to it must be defined elsewhere!

要在交互式控制台中执行代码,不需要在模块中实例化调用。在

假设您的模块名为my_module.py,那么您可以在控制台中像这样调用CallDeleteTitanFiles()

from my_module import CallDeleteTitanFiles, TITAN_FILES_PATH
CallDeleteTitanFiles(TITAN_FILES_PATH)

相关问题 更多 >