aboutsummaryrefslogtreecommitdiff
path: root/src/philo
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-10 15:35:43 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-10 15:35:43 +0100
commit02f56957c2043f0da729be43fcc9a5b985ed30ca (patch)
tree4615197e8fc537f9215d0b69976e7825dd6a3b06 /src/philo
parentd5c0cd45ea8966416ebf0d62ae8320ce54fde501 (diff)
parent41bdcdb45b0903b974a45094c55462951e05980f (diff)
downloadphilosophers_test-02f56957c2043f0da729be43fcc9a5b985ed30ca.tar.gz
philosophers_test-02f56957c2043f0da729be43fcc9a5b985ed30ca.tar.bz2
philosophers_test-02f56957c2043f0da729be43fcc9a5b985ed30ca.zip
Merge branch 'master' of cacharle.xyz:/srv/git/philosophers_testHEADmaster
Diffstat (limited to 'src/philo')
-rw-r--r--src/philo/philo.py11
-rw-r--r--src/philo/table.py18
2 files changed, 14 insertions, 15 deletions
diff --git a/src/philo/philo.py b/src/philo/philo.py
index 0e62e97..09bbc64 100644
--- a/src/philo/philo.py
+++ b/src/philo/philo.py
@@ -6,7 +6,7 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/01 10:52:56 by cacharle #+# #+# #
-# Updated: 2021/01/03 13:31:54 by cacharle ### ########.fr #
+# Updated: 2021/01/10 15:35:14 by cacharle ### ########.fr #
# #
# ############################################################################ #
@@ -25,7 +25,7 @@ class Philo:
timeout_sleep: int,
meal_num: int
):
- self.logs = []
+ self.logs = []
self.id = id_
self._timeout_die = timeout_die
self._timeout_eat = timeout_eat
@@ -56,9 +56,6 @@ class Philo:
# check log event number
grouped = [(e, list(g)) for e, g in itertools.groupby(self.logs, (lambda x: x.event))]
for e, g in grouped[:-1]:
- # if e is Event.EAT:
- # if len(g) != self._meal_num:
- # self._raise("Should eat {} times".format(self._meal_num))
if e is Event.FORK:
if len(g) != 2:
self._raise("Should take fork 2 times")
@@ -127,3 +124,7 @@ class Philo:
else:
return 1
return 0
+
+ @property
+ def meal_num_finished(self):
+ return len([log for log in self.logs if log.event is Event.EAT]) >= self._meal_num
diff --git a/src/philo/table.py b/src/philo/table.py
index 66eaa9d..804608b 100644
--- a/src/philo/table.py
+++ b/src/philo/table.py
@@ -6,13 +6,10 @@
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/10/01 10:53:29 by cacharle #+# #+# #
-# Updated: 2020/10/05 14:03:35 by cacharle ### ########.fr #
+# Updated: 2020/12/30 14:26:37 by charles ### ########.fr #
# #
# ############################################################################ #
-
-import itertools
-
from philo import Philo
from philo import error
from philo.event import Event
@@ -31,20 +28,21 @@ class Table:
for id_ in range(1, philo_num + 1)]
self._logs = []
self._philo_num = philo_num
- self.dead = False
+ self.finished = False
+ self._meal_num = meal_num
def add_log(self, log):
""" Add a log to the correct philosopher
- Set the dead flag if it's a death log
+ Set the finished flag if it's a death log
"""
self._logs.append(log)
philo = next(p for p in self._philos if p.id == log.id)
philo.logs.append(log)
# move
- if self.dead:
+ if self.finished:
raise error.Log(self._logs, "Output after death")
- if log.event is Event.DIE:
- self.dead = True
+ if log.event is Event.DIE or all(p.meal_num_finished for p in self._philos):
+ self.finished = True
def check(self):
""" Check global logs and all philosophers logs for errors
@@ -54,7 +52,7 @@ class Table:
- Timestamps should be in increasing order
"""
- if self.dead:
+ if self.finished:
return
for p in self._philos:
p.check()