forked from PacktPublishing/AdvancedPythonProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample4.py
More file actions
36 lines (28 loc) · 844 Bytes
/
example4.py
File metadata and controls
36 lines (28 loc) · 844 Bytes
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
# ch4/example4.py
import threading
class acquire(object):
def __init__(self, *locks):
self.locks = sorted(locks, key=lambda x: id(x))
def __enter__(self):
for lock in self.locks:
lock.acquire()
def __exit__(self, ty, val, tb):
for lock in reversed(self.locks):
lock.release()
return False
# The philosopher thread
def philosopher(left, right):
while True:
with acquire(left,right):
print(f'Philosopher at {threading.currentThread()} is eating.')
# The chopsticks
N_FORKS = 5
forks = [threading.Lock() for n in range(N_FORKS)]
# Create all of the philosophers
phils = [threading.Thread(
target=philosopher,
args=(forks[n], forks[(n + 1) % N_FORKS])
) for n in range(N_FORKS)]
# Run all of the philosophers
for p in phils:
p.start()