V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lianghui
V2EX  ›  Python

分享 Zephyr : 用 tornado 重写以前一个 flask 的煎蛋博客网站

  •  
  •   lianghui ·
    whiteclover · 2015-11-01 12:48:41 +08:00 · 3027 次点击
    这是一个创建于 3105 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Zephyr 连接地址:
    https://github.com/whiteclover/Zephyr

    一些改进:

    • 使用 hocon 配置文件
    • 全命令行参数控制
    • 使用随机盐加密密码
    • 简单的自动路由系统和 oo 领域

    前端能力有限,没有变动,勿吐槽。适合新手学习,有兴趣的可以 fork 自己改造。

    img

    以前 flask 版本

    https://github.com/whiteclover/white

    $ python zephyrd -h
    
    usage: zephyrd [-h] [--asset.url_prefix ASSET.URL_PREFIX]
                   [--asset.path ASSET.PATH] [--db.db DB.DB] [--db.host DB.HOST]
                   [--db.user DB.USER] [--db.passwd DB.PASSWD] [--db.port DB.PORT]
                   [--jinja2.cache_path JINJA2.CACHE_PATH]
                   [--jinja2.cache_size JINJA2.CACHE_SIZE] [--jinja2.auto_reload]
                   [--redis.host REDIS.HOST] [--redis.port REDIS.PORT]
                   [--redis.db REDIS.DB] [--redis.password REDIS.PASSWORD]
                   [--redis.max_connections REDIS.MAX_CONNECTIONS]
                   [-H TORNADO.HOST] [-p TORNADO.PORT] [-d] [--language LANGUAGE]
                   [--theme THEME] [--secert_key SECERT_KEY] [-c FILE]
                   [-v VERSION]
    
    optional arguments:
      -h, --help            show this help message and exit
    
    Asset settings:
      --asset.url_prefix ASSET.URL_PREFIX
                            Asset url path prefix: (default '/assets/')
      --asset.path ASSET.PATH
                            Asset files path (default
                            '/code/Zephyr/zephyr/asset')
    
    DB settings:
      --db.db DB.DB         The database name (default 'zephyr')
      --db.host DB.HOST     The host of the database (default 'localhost')
      --db.user DB.USER     The user of the database (default 'zephyr')
      --db.passwd DB.PASSWD
                            The password of the database (default 'zephyr')
      --db.port DB.PORT     The port of the database (default 3306)
    
    Jinja2 settings:
      --jinja2.cache_path JINJA2.CACHE_PATH
                            Jinja2 cache code byte path: (default None)
      --jinja2.cache_size JINJA2.CACHE_SIZE
                            Jinja2 cache size: (default -1)
      --jinja2.auto_reload  Jinja2 filesystem checks (default False)
    
    Redis settings:
      --redis.host REDIS.HOST
                            The host of the redis (default 'localhost')
      --redis.port REDIS.PORT
                            The port of the redis (default 6379)
      --redis.db REDIS.DB   The db of the redis (default 0)
      --redis.password REDIS.PASSWORD
                            The user of the redis (default None)
      --redis.max_connections REDIS.MAX_CONNECTIONS
                            The max connections of the redis (default None)
    
    Service settings:
      -H TORNADO.HOST, --tornado.host TORNADO.HOST
                            The host of the tornado server (default 'euterpe')
      -p TORNADO.PORT, --tornado.port TORNADO.PORT
                            The port of the tornado server (default 8888)
      -d, --debug           Open debug mode (default False)
      --language LANGUAGE   The language for the site (default 'en_GB')
      --theme THEME         The theme for the site (default 'default')
      --secert_key SECERT_KEY
                            The secert key for secure cookies (default
                            '7oGwHH8NQDKn9hL12Gak9G/MEjZZYk4PsAxqKU4cJoY=')
      -c FILE, --config FILE
                            config path (default '/etc/zephyr/app.conf')
      -v VERSION, --version VERSION
                            Show zephyr version 0.1.0a
    

    Setup Config file

    Currently, using hocon config. the primary goal of hocon is: keep the semantics (tree structure; set of types; encoding/escaping) from JSON, but make it more convenient as a human-editable config file format.

    # Zehpyr config
    
    
    tornado {
            host = "localhost"
            port = 8888
    }
    
    # theme = "default"
    # languge = "en_GB"
    
    secert_key = "7oGwHH8NQDKn9hL12Gak9G/MEjZZYk4PsAxqKU4cJoY="
    
    debug = off
    
    db {
            passwd = "thomas"
        user = "root"
        host = "localhost"
        db = "zephyr"
    }
    
    
    redis {
            host = "localhost"
            port = 6379
    }
    
    //asset {
    //      url_prefix = "/assets/" // asset url path prefix
    //      path  = "./nodejs/dist/assets" # static files path
    //}
    
    
    jinja2 {
            cache_path = "./cache" # mako module cache  path, comments it if wanna  disable
            auto_reload = on
    }
    
    7 条回复    2015-11-04 17:45:04 +08:00
    jodoo
        1
    jodoo  
       2015-11-01 17:21:37 +08:00
    谢谢分享!对于我这种总是偏执于研究一种轮子的不同实现方法的人来说,很有参考价值。;-)
    sujin190
        2
    sujin190  
       2015-11-01 20:33:38 +08:00
    完全没用 tornado 的异步啊?
    lianghui
        3
    lianghui  
    OP
       2015-11-01 21:14:08 +08:00
    @jodoo 关键是找到更好的解决方法

    @sujin190 你是说那个 wsgi cherrypy server 的包裹, 如果要说异步,应该是在 tornado 的 http io 层是异步的, 其他数据库都不是异步的,如果要做异步 mapper 层和 session 都要重写,另外架构结构也不会是现在这样了。这些没有重数据库的请求,加上 cache 层,前面挂 nginx 后面 tornado app perfork 不是挺好吗
    fire5
        4
    fire5  
       2015-11-02 10:54:17 +08:00
    @lianghui 没有到异步虽然没用到 tornado 的核心特性。不过项目开发来说 没有什么大问题。
    mulog
        5
    mulog  
       2015-11-02 18:17:47 +08:00
    请问用 tornado 重写和和以前用 flask 相比有啥感受?
    lianghui
        6
    lianghui  
    OP
       2015-11-02 21:33:20 +08:00   ❤️ 1
    @fire5 绝对看过 tornado 开发者的观点 ,赞同 https://github.com/tornadoweb/tornado/wiki/Threading-and-concurrency

    1) Do it synchronously and block the IOLoop. This is most appropriate for things like memcache and database queries that are under your control and should always be fast. If it's not fast, make it fast by adding the appropriate indexes to the database, etc.

    @mulog Tornado 提供的一个异步 io 底层库和潦草不多的外层异步库支持,所以也没怎么考虑使用异步 io 数据操作库。 简单的 web 核心 拓展起来比较自由, 个人工作上的项目风格都是面向对象的,现在主要使用 java/scala 做日常工作,所以写的 oo 风格更为习惯一点。 另外令人费解的有人天天吐槽 tornado 为什么不用异步的 io 库或者又没好使的异步数据库 io 库,如果写一些无数据库 io 的异步服务 tornado 绝对是首选。为何就没人吐槽 flask 的 thread local 的线程本地变量隔离机制。 flask 绝对适合上手,拓展性还不错。 同样 tornado 也非常适合上手,但是拓展的话,需要一些时间定制化。

    flask 的拓展被开发者给了一个规范,但不等同于那些拓展都是你需要的,或者能满足你或者需求。 flask 路由上语法糖,相比 routes 那个库,还是觉得 routes 的更为方便点。在 zephyr 开发中加了一些简化命名 url 参数的工具,个人还是习惯像 django 那样把 url 入口写在一块。 zephyr 使用不少 字符串命名惯例 以便实现自动绑定,另方面解决 flask 多人开发造成个别文件被不同开发者修改的一些冲突。 最主要的能够使用更多面向对象风格,自由拓展,驾驭感。
    fire5
        7
    fire5  
       2015-11-04 17:45:04 +08:00
    @lianghui 是的,真看过。 目前我们项目 2 台机器,每个 4 个 tornado 进程,前端 nginx upstream 分发,毫无问题,未来碰到性能问题,第一解决方案还是优化业务逻辑和 sql 。第二方案水平扩展机器。 第三才是异步化操作。第四也是比较终极方案了 elasticsearch 搜索, es 本身提供 http api ,用 tornado 的异步 httpclient 非常容易包装。

    结论:思考可以复杂点,实现简单点搞。性能没那么可怕,也没来的那么快。(普通 web 应用)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2079 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:22 · PVG 09:22 · LAX 18:22 · JFK 21:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.