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

管理多个数据库,利用循环来连接数据库,但是其中一个数据库无法访问,程序就结束了。

  •  
  •   zyqf · 2016-05-26 22:15:38 +08:00 · 2076 次点击
    这是一个创建于 2892 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def loading_config(): 
        try:
                ts = DBconfig.RUN_TIME
                while True:
                        i=0
                        while DBconfig.DBS[i]:
                                head = DBconfig.DBS[i][0]
                                print '-----HOST   :',head[:3],'------------------------'
                                dhost    = DBconfig.DBS[i][0]
                                dport    = DBconfig.DBS[i][1]
                                duser    = DBconfig.DBS[i][2]
                                dpass    = DBconfig.DBS[i][3]
                                ddb      = DBconfig.DBS[i][4]
                                create_mysql(dhost,dport,duser,dpass,ddb,head)
                                main_function(dhost,dport,duser,dpass,ddb,head)
                                print '-----US-time:',time.ctime(),'---'
                                i +=1
                        time.sleep(ts)
        except Exception,e:
            print 'loading_config error!',e
    

    贴上代码那一刻,突然发现自己好蠢.....

    已解决:

    def loading_config(): 
        ts = DBconfig.RUN_TIME
        while True:
                i=0
                while DBconfig.DBS[i]:
                        head = DBconfig.DBS[i][0]
                        print '-----HOST   :',head[:3],'------------------------'
                        dhost    = DBconfig.DBS[i][0]
                        dport    = DBconfig.DBS[i][1]
                        duser    = DBconfig.DBS[i][2]
                        dpass    = DBconfig.DBS[i][3]
                        ddb      = DBconfig.DBS[i][4]
                        try:
                            create_mysql(dhost,dport,duser,dpass,ddb,head)
                            main_function(dhost,dport,duser,dpass,ddb,head)
                        except Exception,e:
                            print 'loading_config error'
                            print e
                        print '-----US-time:',time.ctime(),'---'
                        i +=1
                time.sleep(ts)
    
    

    不过回到最初的代码,好像只要异常发生就不能继续循环了?无解吗....

    4 条回复    2016-05-27 16:16:29 +08:00
    knightdf
        1
    knightdf  
       2016-05-26 22:32:07 +08:00
    catch and then continue
    msg7086
        2
    msg7086  
       2016-05-27 10:00:55 +08:00
    你 catch 在最外层,当然会被你跳出循环了
    kideny
        3
    kideny  
       2016-05-27 13:47:01 +08:00
    这应该有很多种写法吧!
    zyqf
        4
    zyqf  
    OP
       2016-05-27 16:16:29 +08:00
    @knightdf continue 写在 except 下会提示 no loop
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2691 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 00:18 · PVG 08:18 · LAX 17:18 · JFK 20:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.