推荐学习书目
› 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
linzhi
V2EX  ›  Python

Python 一个性能优化问题

  •  
  •   linzhi · Apr 18, 2014 · 6322 views
    This topic created in 4545 days ago, the information mentioned may be changed or developed.
    假设有100个脚本 001.py ... 100.py(就100个单独的文件,不合并)均要调用同一个脚本dst.py, dst.py中的对象初始化要花费不少时间(大概30秒),

    100个脚本执行完毕,那么dst.py中的对象就频繁初始化,开销很大,如何做到 dst.py中的对象只初始化一次呢!!!

    设计模式?设计模式?
    12 replies  •  1970-01-01 08:00:00 +08:00
    yuelang85
        1
    yuelang85  
       Apr 18, 2014   ❤️ 1
    如果你是同一个进程,比如这样:

    a.py是主程序文件,运行后负责调用1.py这些。而1.py,2.py又引入了dst.py,那么dst.py实际只初始化了一次。在同一个进程内,如果没有特殊操作,一个模块只初始化一次。


    如果你每个1.py都是独立的主文件分别执行。那么,想想办法看能不能一个主程序去调用这些1~100.py
    linzhi
        2
    linzhi  
    OP
       Apr 18, 2014
    @yuelang85

    如果你每个1.py都是独立的主文件分别执行。那么,想想办法看能不能一个主程序去调用这些1~100.py

    这个可以试试看~~
    leiz
        3
    leiz  
       Apr 18, 2014
    包一个caller
    thwawar
        4
    thwawar  
       Apr 18, 2014
    或许可以把 dst.py 做成Server,那一百个脚本都通过 socket 和它通信。
    dayigu
        5
    dayigu  
       Apr 18, 2014
    在 ipython 中跑
    linzhi
        6
    linzhi  
    OP
       Apr 18, 2014
    @thwawar 目前有个要求是 001.py ... 100.py 代码一点都不改动...
    cute
        7
    cute  
       Apr 18, 2014
    import dst
    for x in range(100):
    ----execfile('%03d.py' % x, globals(), locals())
    wodemyworld
        8
    wodemyworld  
       Apr 18, 2014
    懒.kill()
    fburst
        9
    fburst  
       Apr 18, 2014
    写个py,分别import 100个文件中的执行函数然后执行。

    跟1楼是一样的
    linzhi
        10
    linzhi  
    OP
       Apr 19, 2014
    @fburst
    @yuelang85

    有没有只修改dst.py这个文件的方法....
    duriantang
        11
    duriantang  
       Apr 24, 2014
    可以用pickle把初始化好的对象保存下到磁盘上。
    yuelang85
        12
    yuelang85  
       Apr 27, 2014
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2600 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 05:40 · PVG 13:40 · LAX 22:40 · JFK 01:40
    ♥ Do have faith in what you're doing.