4
Python: 2.7.3

Flask: 0.9

Hi, I want to make car simulator using Apscheduler. Each car has distance column in the database that will be incremented regularly.

Here's the import part

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

and here's the snippet of the code:

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

I did not write the full code because the error happened right after 'debug 1' with the error message: no handlers could be found for logger "apscheduler.scheduler". Google does not help much.

But the scheduler is still running, printing only 'debug1' every 5 seconds. The error message only come out during the first loop.

Anyone know the solution? Thanks before

[UPDATE]

After using logging, I found out it is RunTimeError: working outside of request context. Is there any other solution other than using with app.test_request_context?

hrsetyono
  • 4,474
  • 13
  • 46
  • 80
  • 1
    related: [Python - No handlers could be found for logger “OpenGL.error”](http://stackoverflow.com/questions/345991/python-no-handlers-could-be-found-for-logger-opengl-error) – jfs Sep 07 '12 at 15:38
  • The logging says RunTimeError: working outside of request context. I think it is because I declared the `g.db` in other class. But I also have other class that use `g.db` without error. Any idea? – hrsetyono Sep 07 '12 at 16:08

2 Answers2

4

g is probably a threading.local(). Different threads see different values in it.

g.db is probably assigned a new db connection per request. No current request – no connection.

You could create db connection in move_all_cars() or pass it explicitly as a parameter.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I tried copying the `moveCar()` into the class that declare `g.db` but it still returns the same 'No handlers could be found for logger' error I think I should handle the logger, but I cannot find good and simple solution in Google – hrsetyono Sep 08 '12 at 05:52
  • 1
    to fix 'no handlers' configure logging. After that It will show what actual error you are having. – jfs Sep 09 '12 at 09:05
  • Thanks, the problem is as you said, it works on different thread. So I put the code that connect to database inside the moveCar() – hrsetyono Sep 09 '12 at 13:26
3
from os import getcwd
import logging
logging.basicConfig(
        filename=''.join([getcwd(), '/', 'log_file_name']),
        level=logging.DEBUG,
        format='%(levelname)s[%(asctime)s]: %(message)s'
)

Above did help in my case.

KAcper

  • +1 Thanks for the reply, I already found the answer though. I will try your solution when I have chance – hrsetyono Sep 29 '12 at 12:47