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()