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 如何在请求结束时释放资源?

  •  
  •   cevincheung ·
    cevin · 2015-05-29 17:04:30 +08:00 · 3817 次点击
    这是一个创建于 3255 天前的主题,其中的信息可能已经有所发展或是发生改变。

    学习python中。

    貌似python做web是执行一个daemon,那么是不是说连接数据库什么的资源不会像php那样被自动释放呢?那不就是会有一堆资源被占用?

    del貌似也是在脚本之行结束后才会被执行啊。python应该怎么释放资源?

    12 条回复    2015-05-30 13:30:11 +08:00
    wy315700
        1
    wy315700  
       2015-05-29 17:07:37 +08:00
    什么框架
    yahoo21cn
        2
    yahoo21cn  
       2015-05-29 17:11:45 +08:00
    一般来说数据库连接用完后要手动关闭,连接池也是。
    binux
        3
    binux  
       2015-05-29 17:12:34 +08:00
    为什么要释放?下一个请求来了接着用不就好了,还节约了连接的时间。
    imlonghao
        5
    imlonghao  
       2015-05-29 17:40:17 +08:00 via Android
    数据库链接从来不断开...
    cevincheung
        6
    cevincheung  
    OP
       2015-05-29 19:15:54 +08:00
    @imlonghao
    @binux
    @yahoo21cn

    那python是如何gc的?
    yahoo21cn
        7
    yahoo21cn  
       2015-05-29 19:25:01 +08:00
    每次用完关闭数据库连接和gc无关,搜索个python的数据库操作例子看看就行了。
    tigerstudent
        8
    tigerstudent  
       2015-05-29 21:34:07 +08:00 via Android
    析构函数在对象没有再被引用就执行了。
    一般为了减少连接消耗,数据库连接都是保持全局的吧。
    谷歌Python垃圾回收找不到讲解?
    cevincheung
        9
    cevincheung  
    OP
       2015-05-29 23:18:20 +08:00
    @tigerstudent
    @yahoo21cn

    那貌似就是说可能会存在web应用执行过程中如果数据库宕机了那必须要重启web daemon?因为当数据库宕机的时候web daemon的连接也断掉了(或者在程序里实现数据库断线重连?)
    yahoo21cn
        10
    yahoo21cn  
       2015-05-30 09:26:00 +08:00 via Android
    还真没试过,不过现在除了学习,一般都是直接上连接池的,连接池有重连机制,你不用操心这个
    cevincheung
        11
    cevincheung  
    OP
       2015-05-30 13:27:45 +08:00
    @yahoo21cn 那连接池挂了咋办?
    yahoo21cn
        12
    yahoo21cn  
       2015-05-30 13:30:11 +08:00 via Android
    连接池就是webserver本身
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1200 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:40 · PVG 07:40 · LAX 16:40 · JFK 19:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.