Skip to content

Instantly share code, notes, and snippets.

@VanishMax
Created September 27, 2020 07:23
Show Gist options
  • Save VanishMax/cf1e3759ca4dfb87b62047debdde508b to your computer and use it in GitHub Desktop.
Save VanishMax/cf1e3759ca4dfb87b62047debdde508b to your computer and use it in GitHub Desktop.
Vector clock algorithm for DS Lab8
"""
This is the improved version of the code given in the article
https://towardsdatascience.com/understanding-lamport-timestamps-with-pythons-multiprocessing-library-12a6427881c6
Shows three communicating processes with the vector clock for synchronization.
Result of running this code is:
Final vector in A is [7, 8, 3]
Final vector in B is [4, 8, 3]
Final vector in C is [1, 5, 4]
"""
from multiprocessing import Process, Pipe
from os import getpid
from datetime import datetime
from time import sleep
def final_count(pid, counter):
name = ''
if (pid == 0):
name = 'A'
elif (pid == 1):
name = 'B'
elif (pid == 2):
name = 'C'
print('Final vector in {} is {}'.format(name, counter))
# Calculate the weights of the vector during the requests
def calc_recv_timestamp(recv_time_stamp, counter):
for id in range(len(counter)):
counter[id] = max(recv_time_stamp[id], counter[id])
return counter
# Custom event inside the process
def event(pid, counter):
counter[pid] += 1
return counter
# Send some text to the neighbor process
def send_message(pipe, pid, counter):
counter[pid] += 1
pipe.send(('Empty shell', counter))
return counter
# Wait for the message from another process with request
def recv_message(pipe, pid, counter):
message, timestamp = pipe.recv()
counter = calc_recv_timestamp(timestamp, counter)
counter[pid] += 1
return counter
# Define processes with their events
def process_a(pipeab):
pid = 0
counter = [0,0,0]
counter = send_message(pipeab, pid, counter)
counter = event(pid, counter)
counter = event(pid, counter)
counter = send_message(pipeab, pid, counter)
counter = event(pid, counter)
counter = recv_message(pipeab, pid, counter)
counter = recv_message(pipeab, pid, counter)
final_count(pid, counter)
def process_b(pipebc, pipeba):
pid = 1
counter = [0,0,0]
counter = recv_message(pipebc, pid, counter)
counter = recv_message(pipebc, pid, counter)
counter = recv_message(pipeba, pid, counter)
counter = event(pid, counter)
counter = send_message(pipebc, pid, counter)
counter = recv_message(pipeba, pid, counter)
counter = send_message(pipeba, pid, counter)
counter = send_message(pipeba, pid, counter)
final_count(pid, counter)
def process_c(pipecb):
pid = 2
counter = [0,0,0]
counter = send_message(pipecb, pid, counter)
counter = event(pid, counter)
counter = send_message(pipecb, pid, counter)
counter = recv_message(pipecb, pid, counter)
final_count(pid, counter)
# Create processes, initialize pipes and make them run
if __name__ == '__main__':
oneandtwo, twoandone = Pipe()
twoandthree, threeandtwo = Pipe()
process1 = Process(target=process_c,
args=(oneandtwo,))
process2 = Process(target=process_b,
args=(twoandone, twoandthree))
process3 = Process(target=process_a,
args=(threeandtwo,))
process1.start()
process2.start()
process3.start()
process1.join()
process2.join()
process3.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment