cjcmay 发表于 2018-8-16 11:09:52

Python 学习笔记 - 上下文

import contextlib  
@contextlib.contextmanager
  
def worker_state(state_list, worker_thread):
  
    state_list.append(worker_thread)
  
    try:
  
      yield
  
    finally:
  
      state_list.remove(worker_thread)
  
      print(state_list)
  

  
free_list = []
  
current_thread = "alex"
  

  
with worker_state(free_list, current_thread):
  
    print(123)
  
    print(456)
  
    print(free_list)
  
----------
  
123
  
456
  
['alex']
  
[]


页: [1]
查看完整版本: Python 学习笔记 - 上下文