在“before_request”(IOError)中使用print时Flask-Application崩溃

时间:2013-11-24 16:23:39

标签: python flask

晚上好!

在我的小瓶子 - 演示应用程序中,我遇到了一个非常奇怪的问题(至少对我而言)。 如果我启动应用程序,做一些数据库的东西等等一切正常。但是一旦我重新启动应用程序(如果重要的话使用PyCharm 3.0),当我尝试时,我得到IOError(OError:[Errno 22]无效的参数)。

我已经确定了它的来源:这是打印声明!

@app.before_request
def before_request():
    g.db = sqlite3.connect(app.config["DATABASE"])
    print "debug: connecting to db" #<--- this causes the application to crash

如果我删除该行,一切都会正常运行。这可能是一个愚蠢的问题,但我不知道为什么会发生这种情况,这真的,真的让我烦恼: - /

编辑:附加信息: Python:ActivePython 2.7,操作系统:Win 8.1

其他相关代码:

配置模块:

DEBUG = True
SECRET_KEY = "testkey"
DATABASE = "dbdb.db"

主要模块:

app = Flask(__name__)
app.config.from_object("config")

@app.before_request
def before_request():
    #print "something" <-- would cause IOError
    g.db = sqlite3.connect(app.config["DATABASE"])

@app.teardown_request
def teardown_request(exception):
    #print "something" <-- would also cause IOError
    db = getattr(g, "db", None)
    if db is not None:
        db.close()

@app.route('/', methods=["GET","POST"])
def admin():
    if request.method == "GET":
        userlist = g.db.execute("SELECT * FROM users").fetchall()
        booklist = g.db.execute("SELECT * FROM books").fetchall()
        return render_template("main.html", userlist = userlist, booklist = booklist)
    elif request.method == "POST":
        pass

if __name__ == '__main__':
    app.run(debug = True)

所以只有一个观点。如上所述,只有当我重新启动应用程序(第一次运行总是顺利)并尝试访问视图时才会出现崩溃。经过(随机?!)的时间后,它会恢复正常。我的猜测是,当我重新启动时,某些数据库连接或类似的东西没有被关闭。

EDIT2:
如果我在代码中留下BOTH print-statements(见上文),会发生什么? 控制台中没有错误消息,只有浏览器向我显示(感谢调试器)一些信息。有趣的是,在我的代码中使用两个print语句时,错误消息在这两个之间交替出现:

首先:

IOError:[Errno 22]参数无效 追溯(最近的呼叫最后一次)

File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app

response = self.make_response(self.handle_exception(e))

File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception

reraise(exc_type, exc_value, tb)

File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app

response = self.full_dispatch_request()

File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request

rv = self.handle_user_exception(e)

File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "C:\Python27\lib\site-packages\flask\app.py", line 1473, in full_dispatch_request

rv = self.preprocess_request()

File "C:\Python27\lib\site-packages\flask\app.py", line 1666, in preprocess_request

 [Display the sourcecode for this frame]  [Open an interactive python shell in this frame] rv = func()

File "C:\Users\MrTrustworthy\PycharmProjects\DB_Flasktest\DB_Flasktest.py", line 31, in before_request

print "print from before_request"

第二

IOError:[Errno 22]参数无效 追溯(最近的呼叫最后一次)

File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

File "C:\Python27\lib\site-packages\flask\app.py", line 1813, in wsgi_app

ctx.push()

File "C:\Python27\lib\site-packages\flask\ctx.py", line 303, in push

top.pop(top._preserved_exc)

File "C:\Python27\lib\site-packages\flask\ctx.py", line 341, in pop

self.app.do_teardown_request(exc)

File "C:\Python27\lib\site-packages\flask\app.py", line 1714, in do_teardown_request

rv = func(exc)

File "C:\Users\MrTrustworthy\PycharmProjects\DB_Flasktest\DB_Flasktest.py", line 38, in teardown_request

print "print from teardown_request"

干杯, MT

0 个答案:

没有答案