小编Zac*_*ncy的帖子

如何在Tornado中的其他类/文件中访问我的数据库级功能?

我是龙卷风的新手,我现在正试图通过这个最近的绊脚石.目前我定义了一些数据库变量,并在初始化Application类时实例化处理程序,设置和数据库连接信息.我还有一个基本处理程序类(名为BaseHandler),它为其他类提供了一个简单的数据库接口.我想将我的一些类拆分为其他文件,并将大部分数据库逻辑放在其他类方法中,并为路由保留application.py,并在需要时实例化这些其他类,并将必要的数据传递给他们为数据库.如何从这些其他文件/类中访问此self.db函数?

application.py:

import tornado.database
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

from tornado.options import define, options
from user import User

# Define some startup settings, can be changed through the command line
define("port", default=8888, help="Run on the given HTTP port", type=int)
define("db_host", default="localhost:3306", help="Database host")
define("db_name", default="database_name", help="Database name")
define("db_user", default="user", help="Database username")
define("db_pass", default="password", help="Database password")

class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r"/", MainHandler)
        ]
        settings = dict(
            application_title = u"Test Application",
            template_path = os.path.join(os.path.dirname(__file__), "templates"),
            static_path …
Run Code Online (Sandbox Code Playgroud)

python mysql tornado

7
推荐指数
1
解决办法
3930
查看次数

标签 统计

mysql ×1

python ×1

tornado ×1