Commit 2de157cc authored by 's avatar
Browse files

on s'en sortira pas sans threading

parent b35d3cff
......@@ -26,6 +26,8 @@ TIMEOUT = 0.5
class LamportClient(threading.Thread):
def __init__(self, id, clients):
super().__init__()
self.id = id
self.time = 0
......@@ -81,7 +83,7 @@ class LamportClient(threading.Thread):
while True:
messages = self.channel.consume(
self.reception, inactivity_timeout=TIMEOUT)
self.reception, inactivity_timeout=TIMEOUT, no_ack=True)
mess = next(messages)
if mess is not None:
......@@ -137,7 +139,7 @@ class LamportClient(threading.Thread):
request access to critical section
"""
self.queue.append((self.id, self.time))
self.time += 1
message = "{},R,{}".format(self.id, self.time)
self.publish(message)
......@@ -175,7 +177,6 @@ class LamportClient(threading.Thread):
self.update_times(sender, time)
def when_receive(self, channel, method, properties, body):
body = str(body)
_,body,_ = body.split("'")
......@@ -209,24 +210,26 @@ class LamportClient(threading.Thread):
print("{} : Ressource released".format(self.id))
def to_acquire(self):
return
def critical_section(client):
print("{} in critical section...".format(client.id))
for i in range(5):
print(" "*(5-i)+"."*i)
time.sleep(1,2)
time.sleep(random.randint(1,5))
if __name__ == "__main__":
id = int(sys.argv[1])
clients = int(sys.argv[2])
client = LamportClient(id, clients)
client.run()
print("running client thread")
client.start()
print("client thread lauched")
while True:
time.sleep(random.randint(1,5))
client.request()
while not client.can_acquire():
time.sleep(1,5)
time.sleep(random.randint(1,5))
print("still can't acquire...")
client.to_acquire()
critical_section()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment