【发布时间】:2012-09-01 12:26:56
【问题描述】:
Python: 2.7.3
Flask: 0.9
您好,我想使用Apscheduler 制作汽车模拟器。每辆车在数据库中都有distance 列,会定期递增。
这是导入部分
from __future__ import with_statement
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash, views
from sqlite3 import dbapi2 as sqlite3
from contextlib import closing
from apscheduler.scheduler import Scheduler
这是代码的sn-p:
sched = Scheduler()
sched.add_interval_job(moveAllCars, seconds = 5)
sched.start()
def moveAllCars():
print "debug 1"
dbCommand = g.db.execute('SELECT CarID FROM Car')
print "debug 2"
#and so on
我没有编写完整的代码,因为错误发生在“调试 1”之后,并显示错误消息:找不到记录器“apscheduler.scheduler”的处理程序。 Google 帮不上什么忙。
但调度程序仍在运行,每 5 秒仅打印一次“debug1”。错误信息仅在第一个循环期间出现。
有人知道解决办法吗?谢谢之前
[更新]
使用logging 后,我发现它是 RunTimeError: working outside of request context。除了使用app.test_request_context之外还有其他解决方案吗?
【问题讨论】:
-
日志显示 RunTimeError:在请求上下文之外工作。我认为这是因为我在其他班级中声明了
g.db。但我也有其他类使用g.db没有错误。有什么想法吗?