-
Notifications
You must be signed in to change notification settings - Fork 2
/
lock_example.py
52 lines (38 loc) · 1.36 KB
/
lock_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Demonstrates Lock from threading used as Context Manager
Similar to File, Lock should also be used as Context Manager to release lock if there is any exception
"""
from threading import Lock
# Example 1, where in Lock is used without context manager
class Task:
def __init__(self):
self.__lock = Lock()
def __do_some_uncertain_task(self):
# Acquires lock
self.__lock.acquire()
raise Exception('some error you knew may happen!')
# Code here is unreachable as before lock release the exception is raised
self.__lock.release()
def initiate(self):
try:
self.__do_some_uncertain_task()
except:
print('Caught exception')
# Here the exceptation is to acquire the lock,
# but since it is never release, code cannot proceed further
self.__lock.acquire()
class BetterTask:
def __init__(self):
self.__lock = Lock()
def __do_some_uncertain_task(self):
with self.__lock:
raise Exception('some error you knew may happen!')
def initiate(self):
try:
self.__do_some_uncertain_task()
except:
print('Caught exception')
self.__lock.acquire()
# Code execution finishes gracefully
BetterTask().initiate()
# Code execution hangs, unable to acquire lock
Task().initiate()