推荐学习书目
› 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 · 6528 views
    This topic created in 4584 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
    yuelang85
    Apr 18, 2014❤️1
    1
    如果你是同一个进程,比如这样:

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


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

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

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

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

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