0

当我有一个except in时memcache.Client,我可以捕获异常,但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)
4

1 回答 1

0
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"
于 2013-09-13T06:42:04.710 回答