簡體   English   中英

python sys.exit不能破壞主要

[英]python sys.exit can't break the main

當我在memcache.Client有一個except時,我可以捕獲該異常,但是mc.getstats仍然是exec,當發生異常時,我應該怎么做才能停止main?

def main():
    if 'host' not in dir():
        host = '127.0.0.1'
    if 'port' not in dir():
        port = '11211'

    server = host + ':' + port

    try:
        mc = memcache.Client([server], debug=1,socket_timeout=3)
        result = mc.get_stats()
        mcstat = result[0][0]
        print mcstat
    except Exception,e:
        print e
        sys.exit(3)

if __name__ == "__main__":
    try:
        main()
    except:
        sys.exit(2)
import memcache
import sys


def main():
    host = "127.0.0.1"
    port = 11211

my_server = "{}:{}".format(host, port)

try:
    mc = memcache.Client(
       my_server   #<**** Should be an array
    )

    result = mc.get_stats()
    mcstat = result[0][0]
    print mcstat
except ValueError, e:
    print "Mission control: There was a problem..."
    print e
    sys.exit(3)

--output:--
Mission control: There was a problem...
Unable to parse connection string: "1"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM