推荐学习书目
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 · May 26, 2016 · 2549 views
    This topic created in 3642 days ago, the information mentioned may be changed or developed.
    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 replies    2016-05-27 16:16:29 +08:00
    knightdf
        1
    knightdf  
       May 26, 2016
    catch and then continue
    msg7086
        2
    msg7086  
       May 27, 2016
    你 catch 在最外层,当然会被你跳出循环了
    kideny
        3
    kideny  
       May 27, 2016
    这应该有很多种写法吧!
    zyqf
        4
    zyqf  
    OP
       May 27, 2016
    @knightdf continue 写在 except 下会提示 no loop
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2771 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 01:54 · PVG 09:54 · LAX 18:54 · JFK 21:54
    ♥ Do have faith in what you're doing.