1import threading
2lock = threading.Lock()
3def check_this():
4 with lock:
5 """
6 acquires lock at the beginning
7 and releases at the end of this block
8 """
9
10 a, b = 1, 0
11 print("locked")
12 try:
13 print(a // b)
14 except Exception as _:
15 print(_)
16 print("lock is released")
17
18[threading.Thread(target=also_this).start() for _ in range(2)]