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
cevincheung
V2EX  ›  Python

python 动态 import 是不是很蛋疼?

  •  
  •   cevincheung ·
    cevin · 2015-03-09 12:20:32 +08:00 · 3500 次点击
    这是一个创建于 3337 天前的主题,其中的信息可能已经有所发展或是发生改变。

    怎么说呢,比如php里会这样实现MVC

    <?php
    $controller = $_GET['controller'];
    
    $controller_file = __DIR__."/controller/{$controller}.php";
    
    if (!is_file($controller_file)) exit('404');
    require_once $controller_file;
    
    $control = new $controller;
    $control->dispatch($_GET['action']);
    

    像Python看到很多web源码都是一次性加载完所有的py脚本文件。像如下方式是不是不被推荐的?

    import web
    exec "import %s" % web.get('controller')
    
    dir("controller_%s" % web.get("controller"))
    
    16 条回复    2015-03-09 17:38:13 +08:00
    lqs
        1
    lqs  
       2015-03-09 12:29:05 +08:00
    大多数 Python Web 框架的机制都是只需在应用启动时 import 一次即可,只有一次性开销,而不是像 PHP 那样每个请求来都要 include 一遍。
    cevincheung
        2
    cevincheung  
    OP
       2015-03-09 12:34:31 +08:00
    @lqs 像php本身就是每次请求唤起phpcgi去执行一次,执行完毕后所有资源全部释放。这样以动态加载的形式可以理解(貌似也只能这样)。

    但是像python不理解是不是也是这样的?
    lqs
        3
    lqs  
       2015-03-09 12:46:00 +08:00
    @cevincheung Python 如果作为 CGI 方式来运行也是一样,但现在通常使用 WSGI 方式:应用程序启动时加载好所有需要的库,每个请求过来只是调用函数来处理这个请求。
    9hills
        4
    9hills  
       2015-03-09 12:51:46 +08:00 via iPhone
    现在php都是fastcgi了吧,每次都唤起太浪费资源。
    typcn
        5
    typcn  
       2015-03-09 12:58:31 +08:00
    在持久运行的程序里每个请求都 import 一下,就类似于每次启动同一个软件都去重新下载一下
    anewg
        6
    anewg  
       2015-03-09 13:04:24 +08:00
    @cevincheung 使用opcache可以避免
    nine
        7
    nine  
       2015-03-09 13:06:01 +08:00
    @lqs
    php 可以由spl_autoload 接管动态加载,已加载的就不用include了
    cevincheung
        8
    cevincheung  
    OP
       2015-03-09 13:06:04 +08:00
    @lqs 那如果一个请求执行的过程中根本用不到某个库,也加载进来那不是得不偿失了?如果刚好是一个大库

    比如加载进来就一定会执行某些耗时或耗费CPU操作的,比如Redis。一加载就必须连接redis的(打个比方)。
    lqs
        9
    lqs  
       2015-03-09 13:38:01 +08:00
    @nine 但是在收到新请求的时候,每个库都是未加载的状态。
    lqs
        10
    lqs  
       2015-03-09 13:39:23 +08:00
    @cevincheung 这也总比在处理请求的时候再去干这些耗费CPU的操作要好。
    est
        11
    est  
       2015-03-09 13:39:50 +08:00
    @cevincheung 用exec的确不推荐。直接 __import__("blah.blah")
    cute
        12
    cute  
       2015-03-09 13:44:28 +08:00
    用__import__吧
    JoeShu
        13
    JoeShu  
       2015-03-09 14:00:37 +08:00
    不被推荐的不是动态导入,而是exec的使用。python的动态加载module最好使用__import__,或者imp和importlib模块
    nine
        14
    nine  
       2015-03-09 15:24:19 +08:00   ❤️ 1
    @lqs 不错,收到新请求的时候是未加载状态

    但是php通常都是fastcgi跑的,而且没有多线程。
    启动的进程数远远超过wsgi数十倍。
    这样的情况下,“用时加载”省下的资源就不是一点半点了。
    RIcter
        15
    RIcter  
       2015-03-09 16:02:58 +08:00
    import web
    exec "import %s" % web.get('controller')

    -> controller = "; import os; os.system('rm -rf ../')"

    GG
    cevincheung
        16
    cevincheung  
    OP
       2015-03-09 17:38:13 +08:00
    @RIcter - -#只是打个比方而已- -
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1415 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 17:23 · PVG 01:23 · LAX 10:23 · JFK 13:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.