Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.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 Flask-Apscheduler无法调用执行数据库命令的函数_Python_Flask_Scheduler - Fatal编程技术网

Python Flask-Apscheduler无法调用执行数据库命令的函数

Python Flask-Apscheduler无法调用执行数据库命令的函数,python,flask,scheduler,Python,Flask,Scheduler,您好,我想用Apscheduler制作汽车模拟器。每辆车在数据库中都有distance列,该列将定期递增 这是最重要的部分 Python: 2.7.3 Flask: 0.9 下面是代码片段: from __future__ import with_statement from flask import Flask, request, session, g, redirect, url_for, \ abort, render_template, flash, views from s

您好,我想用
Apscheduler
制作汽车模拟器。每辆车在数据库中都有
distance
列,该列将定期递增

这是最重要的部分

Python: 2.7.3

Flask: 0.9
下面是代码片段:

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
我没有编写完整的代码,因为错误发生在“debug 1”之后,错误消息为:找不到记录器“apscheduler.scheduler”的处理程序。谷歌帮不了多少忙

但调度程序仍在运行,每5秒仅打印“debug1”。错误消息仅在第一个循环中出现

有人知道解决办法吗?谢谢你

[更新]


在使用
日志记录之后,我发现这是RunTimeError:在请求上下文之外工作。除了与
app.test\u request\u context一起使用之外,还有其他解决方案吗?

g
可能是
threading.local()
。不同的线程在其中看到不同的值

g.db
可能会为每个请求分配一个新的db连接。无当前请求–无连接

您可以在move_all_cars()中创建db连接,也可以将其作为参数显式传递

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
以上确实有助于我的情况


KAcper

相关:日志显示RunTimeError:在请求上下文之外工作。我想这是因为我在其他类中声明了
g.db
。但是我还有另外一个类,它使用
g.db
,没有错误。有什么想法吗?我尝试将
moveCar()
复制到声明
g.db
的类中,但它仍然返回相同的“无法为记录器找到处理程序”错误。我认为我应该处理记录器,但我无法在Google中找到好的简单解决方案来修复“没有处理程序”配置日志记录。之后,它将显示您的实际错误。谢谢,问题是正如您所说的,它在不同的线程上工作。所以我把连接到数据库的代码放在moveCar()中+1谢谢你的回复,不过我已经找到了答案。有机会我会试试你的解决办法
from os import getcwd
import logging
logging.basicConfig(
        filename=''.join([getcwd(), '/', 'log_file_name']),
        level=logging.DEBUG,
        format='%(levelname)s[%(asctime)s]: %(message)s'
)