您的位置:首页 > 编程语言 > Python开发

当python finally遇到break和sys.exit

2011-12-05 15:47 423 查看
except lite.Error, e:
print "Error %s:" % e.args[0]
sys.exit(1)
print '++++++++++++++++++++++'
finally:
print "---------------"


finally会不会执行?

在google搜:sys.exit finally第一个:

http://stackoverflow.com/questions/7709411/why-finally-block-is-executing-after-calling-sys-exit0-in-except-block

All
sys.exit()
does
is raise an exception of type
SystemExit
.!

python很聪明的!

另外一种情况在循环中break

import Queue
import threading
import time
import sys

workQueue = Queue.Queue(10)

for i in range(0,6):
workQueue.put(i)

def thread_get():
print 'i am starting------'
while  True:
try:
req = workQueue.get()
break
print 'aaaaaaaaa'
except Exception,qe:
print 'enmpty-----------'
break
finally:
print 'i finally'
workQueue.task_done()

for i in range(0,5):
t = threading.Thread(target=thread_get)
t.setDaemon(True)
t.start()

workQueue.join()
print "Exiting Main Thread"


Py官网说的:When a return, break or continue statement
is executed in the try suite
of a try...finally statement,
the finally clause
is also executed ‘on the way out.’ A continue statement
is illegal in the finally clause.
(The reason is a problem with the current implementation — this restriction may be lifted in the future).

经过测试break后退出,造成task_done 死等
第一次测试竟然通过,,细查只正好放了5个数据进Queue!改成6个死锁!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: