# 定义一个全局变量 lock = threading.Lock() # 定义一个函数,用于模拟悲观锁 def pessimistic_lock(): # 获取锁 lock.acquire() try: # 执行操作 print("Do something") finally: # 释放锁 lock.release() # 调用函数 pessimistic_lock()