GAE任务队列:任务未运行/输出

1 投票
1 回答
612 浏览
提问于 2025-04-16 14:59

我开始使用任务队列来安排一些需要花时间的任务在后台运行。我想运行的任务在网址 '/test',而我用来安排这个任务的网址是 '/bgtest'。下面是 '/bgtest' 的处理程序:

class RunTestAsBackgroundProcess(BaseHandler):
def get_secure(self):
    taskqueue.add(url='/test', method='GET')
    logging.debug("Task added to queue")
    return

这个 '/test' 任务会把数据输出到日志中,当我正常访问 /test 时,它会执行完毕,我可以在日志中找到结果。然而,当我运行 /bgtest 时,日志里除了上面函数的“任务已添加到队列”的消息外,什么也没有。奇怪的是,在管理控制台的任务队列里显示最近一分钟内有任务运行,但没有给我任何详细信息。有没有什么想法?

补充说明一下代码,BaseHandler 是我用来检查用户是否登录 Facebook 的一个父类,而 get_secure() 是在父类的 get() 方法之后调用的方法。

补充说明:/test 运行这个类:

class CalculateTestAllocations(BaseHandler):
def get_secure(self):
    dbuser = db.GqlQuery("SELECT * FROM User WHERE fbid = :1", self.user['uid'])[0]
    if (dbuser.isadmin != True):
        self.redirect('/')

    #test data
    drivers = []
    passengers = []

    drivers.append(allocation.Driver("01", allocation.Location(51.440958, -2.576318), 3, 1000)) # coming from Bristol
    drivers.append(allocation.Driver("02", allocation.Location(55.935628, -3.285044), 3, 1000)) # coming from Edinburgh

    passengers.append(allocation.Passenger("03", allocation.Location(51.483193, -3.208187), 1000)) # coming from Cardiff
    passengers.append(allocation.Passenger("04", allocation.Location(52.469263, -1.860303), 1000)) # coming from Birmingham
    passengers.append(allocation.Passenger("05", allocation.Location(53.783703, -1.541841), 1000)) # coming from Leeds
    passengers.append(allocation.Passenger("06", allocation.Location(54.973994, -1.636391), 1000)) # coming from Newcastle

    logging.debug("Running allocation engine now (GET)")

    alloc = allocation.Allocation()
    alloc.buildProblem(drivers, passengers, allocation.Location(52.951923, -1.169967)) # destination at Nottingham
    alloc.solveAndOutput()

这个类会为我的分配算法生成一组测试数据(这个算法会接收一组司机和乘客,并计算出最佳路线),然后告诉算法开始运行。发送到日志中的内容是在 allocation.solveAndOutput() 方法里完成的,这个方法是这样的:

def solveAndOutput(self):
    routes = self.solveProblem()
    logging.warn("Num routes: "+str(len(routes)))
    logging.warn("Length of first route: "+str(len(routes[0])))
    for route in routes:
        print self.getStaticMapAddress(route)
        logging.debug(self.getStaticMapAddress(route))

正如我所说,如果我只是运行 /test,我会得到这些输出,但如果我运行 /bgtest,除了任务队列显示过去一分钟内运行过什么,其他什么也没有发生。

1 个回答

1

看起来你的 /test 脚本是在获取一个我猜是会话(session)的信息,然后根据这个信息进行重定向。显然,这在任务队列(Task Queue)的任务中是行不通的,因为那里没有用户,也就没有会话。

撰写回答