with上下文管理基础

 1 import queue
 2 import contextlib
 3 import time
 4 
 5 
 6 @contextlib.contextmanager
 7 def worker_state(xxx,val):
 8     xxx.append(val)
 9     print("before",xxx)
10     try:
11         time.sleep(1)
12         yield
13     finally:
14         xxx.remove(val)
15         print("after",xxx)
16 
17         
18 if __name__ == '__main__':
19     q = queue.Queue()
20     q.put("123")
21     li = []
22     with worker_state(li,1):
23         q.get()

 

posted @ 2017-02-23 13:50  失落的黎明  阅读(147)  评论(0编辑  收藏  举报