How to run multiple Replicated socket servers concurrently in python?










0















I'm implementing Lamports Logical clock algorithm in python.Hence, I had to create three processes process1.py, process2.py, process3.py on the same machine and run them parallely at the same time so that each process multicasts events to all other processes. I'm using shell script to run these python files parallely. I'm using sockets for establishing communication between them using different ports. The code of all the three processes remain same except they listen on different ports for messages.
Since all the processes are same and initiate communication at the same time I'm getting an error "Connection Refused". I know that this may occur when server isn't up in case of client-server communication. But in my case, all the components are servers. Any help is greatly appreciated.



process1.py



import socket
import math
import time
import threading

class process1:

# send message to p2 and p3.
def send_message(self):
buffer = [1.1]
ack_buf =
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(True)
sock.bind(('localhost',6000))
sock.connect(('localhost',6001))
message1 = 'payload' : 1.1
sock.send(str(message1).encode('utf8'))
data = sock.recv(1024)
var = eval(data.decode('utf8'))
if 'payload' in var:
buffer.append(var['payload'])
threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
elif 'ack' in var:
ack_buf.append(var['ack'])
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('localhost', 6002))
s.send(str(message1).encode('utf8'))
data_recv = sock.recv(1024)
var = eval(data_recv.decode('utf8'))
if 'payload' in var:
buffer.append(var['payload'])
threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
elif 'ack' in var:
ack_buf.append(var['ack'])
self.print_events(buffer)
self.validate(buffer, ack_buf)

# sends acknowlegment to all the processes in the distributed system.
def send_ack(self,event, socket):
if event>0 and socket is not None:
socket.send(str('ack':event).encode('utf8'))

# prints the events in the sorted order
def print_events(self,buffer):
sorted(buffer)
for x in range(len(buffer)):
m = buffer[x]
frac, whole = math.modf(m)
each_event = int(frac*10)
print('Event %d of process %d is happening according to the received order'%(each_event, whole))

# validates if the msgs sent are received by the processes
def validate(self, list_of_events, recv_buf):
count = 0
if len(list_of_events) > 0 and len(recv_buf) > 0:
for x in range(len(list_of_events)):
if list_of_events[x] in recv_buf:
count += 1
if count == 2:
print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

if __name__ == "__main__":
p1 = process1()
time.sleep(.300)
threading.Thread(target=p1.send_message).start()


process2.py



import socket
import math
import time
import threading

class process2:

# send message to p2 and p3.
def send_message(self):
buffer = [1.1]
ack_buf =
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(True)
sock.bind(('localhost',6001))
time.sleep(.300)
sock.connect(('localhost',6000))
message1 = 'payload' : 2.1
sock.send(str(message1).encode('utf8'))
data = sock.recv(1024)
var = eval(data.decode('utf8'))
if 'payload' in var:
buffer.append(var['payload'])
threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
elif 'ack' in var:
ack_buf.append(var['ack'])
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
time.sleep(.300)
s.connect(('localhost', 6002))
s.send(str(message1).encode('utf8'))
data_recv = sock.recv(1024)
var = eval(data_recv.decode('utf8'))
if 'payload' in var:
buffer.append(var['payload'])
threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
elif 'ack' in var:
ack_buf.append(var['ack'])
self.print_events(buffer)
self.validate(buffer, ack_buf)

# sends acknowlegment to all the processes in the distributed system.
def send_ack(self, event, socket):
if event>0 and socket is not None:
socket.send(str('ack':event).encode('utf8'))

# prints the events in the sorted order
def print_events(self, buffer):
sorted(buffer)
for x in range(len(buffer)):
m = buffer[x]
frac, whole = math.modf(m)
each_event = int(frac*10)
print('Event %d of process %d is happening according to the received order'%(each_event, whole))

# validates if the msgs sent are received by the processes
def validate(self, list_of_events, recv_buf):
count = 0
if len(list_of_events) > 0 and len(recv_buf) > 0:
for x in range(len(list_of_events)):
if list_of_events[x] in recv_buf:
count += 1
if count == 2:
print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

if __name__ == "__main__":
p2 = process2()
time.sleep(.300)
threading.Thread(target=p2.send_message).start()


process3.py



import socket
import math
import time
import threading

class process3:

# send message to p2 and p3.
def send_message(self):
buffer = [1.1]
ack_buf =
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(True)
sock.bind(('localhost',6002))
sock.connect(('localhost',6000))
message1 = 'payload' : 3.1
sock.send(str(message1).encode('utf8'))
data = sock.recv(1024)
var = eval(data.decode('utf8'))
if 'payload' in var:
buffer.append(var['payload'])
threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
elif var.has_key('ack'):
ack_buf.append(var['ack'])
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
time.sleep(.300)
s.connect(('localhost', 6001))
s.send(str(message1).encode('utf8'))
data_recv = sock.recv(1024)
var = eval(data_recv.decode('utf8'))
if 'payload' in var:
buffer.append(var['payload'])
threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
elif 'ack' in var:
ack_buf.append(var['ack'])
self.print_events(buffer)
self.validate(buffer, ack_buf)

# sends acknowlegment to all the processes in the distributed system.
def send_ack(self, event, socket):
if event>0 and socket is not None:
socket.send(str('ack':event).encode('utf8'))

# prints the events in the sorted order
def print_events(self, buffer):
sorted(buffer)
for x in range(len(buffer)):
m = buffer[x]
frac, whole = math.modf(m)
each_event = int(frac*10)
print('Event %d of process %d is happening according to the received order'%(each_event, whole))

# validates if the msgs sent are received by the processes
def validate(self, list_of_events, recv_buf):
count = 0
if len(list_of_events) > 0 and len(recv_buf) > 0:
for x in range(len(list_of_events)):
if list_of_events[x] in recv_buf:
count += 1
if count == 2:
print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

if __name__ == "__main__":
p3 = process3()
time.sleep(.300)
threading.Thread(target=p3.send_message).start()


job.sh



python3 process1.py &
python3 process2.py &
python3 process3.py &










share|improve this question


























    0















    I'm implementing Lamports Logical clock algorithm in python.Hence, I had to create three processes process1.py, process2.py, process3.py on the same machine and run them parallely at the same time so that each process multicasts events to all other processes. I'm using shell script to run these python files parallely. I'm using sockets for establishing communication between them using different ports. The code of all the three processes remain same except they listen on different ports for messages.
    Since all the processes are same and initiate communication at the same time I'm getting an error "Connection Refused". I know that this may occur when server isn't up in case of client-server communication. But in my case, all the components are servers. Any help is greatly appreciated.



    process1.py



    import socket
    import math
    import time
    import threading

    class process1:

    # send message to p2 and p3.
    def send_message(self):
    buffer = [1.1]
    ack_buf =
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setblocking(True)
    sock.bind(('localhost',6000))
    sock.connect(('localhost',6001))
    message1 = 'payload' : 1.1
    sock.send(str(message1).encode('utf8'))
    data = sock.recv(1024)
    var = eval(data.decode('utf8'))
    if 'payload' in var:
    buffer.append(var['payload'])
    threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
    elif 'ack' in var:
    ack_buf.append(var['ack'])
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect(('localhost', 6002))
    s.send(str(message1).encode('utf8'))
    data_recv = sock.recv(1024)
    var = eval(data_recv.decode('utf8'))
    if 'payload' in var:
    buffer.append(var['payload'])
    threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
    elif 'ack' in var:
    ack_buf.append(var['ack'])
    self.print_events(buffer)
    self.validate(buffer, ack_buf)

    # sends acknowlegment to all the processes in the distributed system.
    def send_ack(self,event, socket):
    if event>0 and socket is not None:
    socket.send(str('ack':event).encode('utf8'))

    # prints the events in the sorted order
    def print_events(self,buffer):
    sorted(buffer)
    for x in range(len(buffer)):
    m = buffer[x]
    frac, whole = math.modf(m)
    each_event = int(frac*10)
    print('Event %d of process %d is happening according to the received order'%(each_event, whole))

    # validates if the msgs sent are received by the processes
    def validate(self, list_of_events, recv_buf):
    count = 0
    if len(list_of_events) > 0 and len(recv_buf) > 0:
    for x in range(len(list_of_events)):
    if list_of_events[x] in recv_buf:
    count += 1
    if count == 2:
    print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

    if __name__ == "__main__":
    p1 = process1()
    time.sleep(.300)
    threading.Thread(target=p1.send_message).start()


    process2.py



    import socket
    import math
    import time
    import threading

    class process2:

    # send message to p2 and p3.
    def send_message(self):
    buffer = [1.1]
    ack_buf =
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setblocking(True)
    sock.bind(('localhost',6001))
    time.sleep(.300)
    sock.connect(('localhost',6000))
    message1 = 'payload' : 2.1
    sock.send(str(message1).encode('utf8'))
    data = sock.recv(1024)
    var = eval(data.decode('utf8'))
    if 'payload' in var:
    buffer.append(var['payload'])
    threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
    elif 'ack' in var:
    ack_buf.append(var['ack'])
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    time.sleep(.300)
    s.connect(('localhost', 6002))
    s.send(str(message1).encode('utf8'))
    data_recv = sock.recv(1024)
    var = eval(data_recv.decode('utf8'))
    if 'payload' in var:
    buffer.append(var['payload'])
    threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
    elif 'ack' in var:
    ack_buf.append(var['ack'])
    self.print_events(buffer)
    self.validate(buffer, ack_buf)

    # sends acknowlegment to all the processes in the distributed system.
    def send_ack(self, event, socket):
    if event>0 and socket is not None:
    socket.send(str('ack':event).encode('utf8'))

    # prints the events in the sorted order
    def print_events(self, buffer):
    sorted(buffer)
    for x in range(len(buffer)):
    m = buffer[x]
    frac, whole = math.modf(m)
    each_event = int(frac*10)
    print('Event %d of process %d is happening according to the received order'%(each_event, whole))

    # validates if the msgs sent are received by the processes
    def validate(self, list_of_events, recv_buf):
    count = 0
    if len(list_of_events) > 0 and len(recv_buf) > 0:
    for x in range(len(list_of_events)):
    if list_of_events[x] in recv_buf:
    count += 1
    if count == 2:
    print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

    if __name__ == "__main__":
    p2 = process2()
    time.sleep(.300)
    threading.Thread(target=p2.send_message).start()


    process3.py



    import socket
    import math
    import time
    import threading

    class process3:

    # send message to p2 and p3.
    def send_message(self):
    buffer = [1.1]
    ack_buf =
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setblocking(True)
    sock.bind(('localhost',6002))
    sock.connect(('localhost',6000))
    message1 = 'payload' : 3.1
    sock.send(str(message1).encode('utf8'))
    data = sock.recv(1024)
    var = eval(data.decode('utf8'))
    if 'payload' in var:
    buffer.append(var['payload'])
    threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
    elif var.has_key('ack'):
    ack_buf.append(var['ack'])
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    time.sleep(.300)
    s.connect(('localhost', 6001))
    s.send(str(message1).encode('utf8'))
    data_recv = sock.recv(1024)
    var = eval(data_recv.decode('utf8'))
    if 'payload' in var:
    buffer.append(var['payload'])
    threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
    elif 'ack' in var:
    ack_buf.append(var['ack'])
    self.print_events(buffer)
    self.validate(buffer, ack_buf)

    # sends acknowlegment to all the processes in the distributed system.
    def send_ack(self, event, socket):
    if event>0 and socket is not None:
    socket.send(str('ack':event).encode('utf8'))

    # prints the events in the sorted order
    def print_events(self, buffer):
    sorted(buffer)
    for x in range(len(buffer)):
    m = buffer[x]
    frac, whole = math.modf(m)
    each_event = int(frac*10)
    print('Event %d of process %d is happening according to the received order'%(each_event, whole))

    # validates if the msgs sent are received by the processes
    def validate(self, list_of_events, recv_buf):
    count = 0
    if len(list_of_events) > 0 and len(recv_buf) > 0:
    for x in range(len(list_of_events)):
    if list_of_events[x] in recv_buf:
    count += 1
    if count == 2:
    print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

    if __name__ == "__main__":
    p3 = process3()
    time.sleep(.300)
    threading.Thread(target=p3.send_message).start()


    job.sh



    python3 process1.py &
    python3 process2.py &
    python3 process3.py &










    share|improve this question
























      0












      0








      0








      I'm implementing Lamports Logical clock algorithm in python.Hence, I had to create three processes process1.py, process2.py, process3.py on the same machine and run them parallely at the same time so that each process multicasts events to all other processes. I'm using shell script to run these python files parallely. I'm using sockets for establishing communication between them using different ports. The code of all the three processes remain same except they listen on different ports for messages.
      Since all the processes are same and initiate communication at the same time I'm getting an error "Connection Refused". I know that this may occur when server isn't up in case of client-server communication. But in my case, all the components are servers. Any help is greatly appreciated.



      process1.py



      import socket
      import math
      import time
      import threading

      class process1:

      # send message to p2 and p3.
      def send_message(self):
      buffer = [1.1]
      ack_buf =
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setblocking(True)
      sock.bind(('localhost',6000))
      sock.connect(('localhost',6001))
      message1 = 'payload' : 1.1
      sock.send(str(message1).encode('utf8'))
      data = sock.recv(1024)
      var = eval(data.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      s.connect(('localhost', 6002))
      s.send(str(message1).encode('utf8'))
      data_recv = sock.recv(1024)
      var = eval(data_recv.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      self.print_events(buffer)
      self.validate(buffer, ack_buf)

      # sends acknowlegment to all the processes in the distributed system.
      def send_ack(self,event, socket):
      if event>0 and socket is not None:
      socket.send(str('ack':event).encode('utf8'))

      # prints the events in the sorted order
      def print_events(self,buffer):
      sorted(buffer)
      for x in range(len(buffer)):
      m = buffer[x]
      frac, whole = math.modf(m)
      each_event = int(frac*10)
      print('Event %d of process %d is happening according to the received order'%(each_event, whole))

      # validates if the msgs sent are received by the processes
      def validate(self, list_of_events, recv_buf):
      count = 0
      if len(list_of_events) > 0 and len(recv_buf) > 0:
      for x in range(len(list_of_events)):
      if list_of_events[x] in recv_buf:
      count += 1
      if count == 2:
      print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

      if __name__ == "__main__":
      p1 = process1()
      time.sleep(.300)
      threading.Thread(target=p1.send_message).start()


      process2.py



      import socket
      import math
      import time
      import threading

      class process2:

      # send message to p2 and p3.
      def send_message(self):
      buffer = [1.1]
      ack_buf =
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setblocking(True)
      sock.bind(('localhost',6001))
      time.sleep(.300)
      sock.connect(('localhost',6000))
      message1 = 'payload' : 2.1
      sock.send(str(message1).encode('utf8'))
      data = sock.recv(1024)
      var = eval(data.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      time.sleep(.300)
      s.connect(('localhost', 6002))
      s.send(str(message1).encode('utf8'))
      data_recv = sock.recv(1024)
      var = eval(data_recv.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      self.print_events(buffer)
      self.validate(buffer, ack_buf)

      # sends acknowlegment to all the processes in the distributed system.
      def send_ack(self, event, socket):
      if event>0 and socket is not None:
      socket.send(str('ack':event).encode('utf8'))

      # prints the events in the sorted order
      def print_events(self, buffer):
      sorted(buffer)
      for x in range(len(buffer)):
      m = buffer[x]
      frac, whole = math.modf(m)
      each_event = int(frac*10)
      print('Event %d of process %d is happening according to the received order'%(each_event, whole))

      # validates if the msgs sent are received by the processes
      def validate(self, list_of_events, recv_buf):
      count = 0
      if len(list_of_events) > 0 and len(recv_buf) > 0:
      for x in range(len(list_of_events)):
      if list_of_events[x] in recv_buf:
      count += 1
      if count == 2:
      print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

      if __name__ == "__main__":
      p2 = process2()
      time.sleep(.300)
      threading.Thread(target=p2.send_message).start()


      process3.py



      import socket
      import math
      import time
      import threading

      class process3:

      # send message to p2 and p3.
      def send_message(self):
      buffer = [1.1]
      ack_buf =
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setblocking(True)
      sock.bind(('localhost',6002))
      sock.connect(('localhost',6000))
      message1 = 'payload' : 3.1
      sock.send(str(message1).encode('utf8'))
      data = sock.recv(1024)
      var = eval(data.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
      elif var.has_key('ack'):
      ack_buf.append(var['ack'])
      s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      time.sleep(.300)
      s.connect(('localhost', 6001))
      s.send(str(message1).encode('utf8'))
      data_recv = sock.recv(1024)
      var = eval(data_recv.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      self.print_events(buffer)
      self.validate(buffer, ack_buf)

      # sends acknowlegment to all the processes in the distributed system.
      def send_ack(self, event, socket):
      if event>0 and socket is not None:
      socket.send(str('ack':event).encode('utf8'))

      # prints the events in the sorted order
      def print_events(self, buffer):
      sorted(buffer)
      for x in range(len(buffer)):
      m = buffer[x]
      frac, whole = math.modf(m)
      each_event = int(frac*10)
      print('Event %d of process %d is happening according to the received order'%(each_event, whole))

      # validates if the msgs sent are received by the processes
      def validate(self, list_of_events, recv_buf):
      count = 0
      if len(list_of_events) > 0 and len(recv_buf) > 0:
      for x in range(len(list_of_events)):
      if list_of_events[x] in recv_buf:
      count += 1
      if count == 2:
      print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

      if __name__ == "__main__":
      p3 = process3()
      time.sleep(.300)
      threading.Thread(target=p3.send_message).start()


      job.sh



      python3 process1.py &
      python3 process2.py &
      python3 process3.py &










      share|improve this question














      I'm implementing Lamports Logical clock algorithm in python.Hence, I had to create three processes process1.py, process2.py, process3.py on the same machine and run them parallely at the same time so that each process multicasts events to all other processes. I'm using shell script to run these python files parallely. I'm using sockets for establishing communication between them using different ports. The code of all the three processes remain same except they listen on different ports for messages.
      Since all the processes are same and initiate communication at the same time I'm getting an error "Connection Refused". I know that this may occur when server isn't up in case of client-server communication. But in my case, all the components are servers. Any help is greatly appreciated.



      process1.py



      import socket
      import math
      import time
      import threading

      class process1:

      # send message to p2 and p3.
      def send_message(self):
      buffer = [1.1]
      ack_buf =
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setblocking(True)
      sock.bind(('localhost',6000))
      sock.connect(('localhost',6001))
      message1 = 'payload' : 1.1
      sock.send(str(message1).encode('utf8'))
      data = sock.recv(1024)
      var = eval(data.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      s.connect(('localhost', 6002))
      s.send(str(message1).encode('utf8'))
      data_recv = sock.recv(1024)
      var = eval(data_recv.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      self.print_events(buffer)
      self.validate(buffer, ack_buf)

      # sends acknowlegment to all the processes in the distributed system.
      def send_ack(self,event, socket):
      if event>0 and socket is not None:
      socket.send(str('ack':event).encode('utf8'))

      # prints the events in the sorted order
      def print_events(self,buffer):
      sorted(buffer)
      for x in range(len(buffer)):
      m = buffer[x]
      frac, whole = math.modf(m)
      each_event = int(frac*10)
      print('Event %d of process %d is happening according to the received order'%(each_event, whole))

      # validates if the msgs sent are received by the processes
      def validate(self, list_of_events, recv_buf):
      count = 0
      if len(list_of_events) > 0 and len(recv_buf) > 0:
      for x in range(len(list_of_events)):
      if list_of_events[x] in recv_buf:
      count += 1
      if count == 2:
      print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

      if __name__ == "__main__":
      p1 = process1()
      time.sleep(.300)
      threading.Thread(target=p1.send_message).start()


      process2.py



      import socket
      import math
      import time
      import threading

      class process2:

      # send message to p2 and p3.
      def send_message(self):
      buffer = [1.1]
      ack_buf =
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setblocking(True)
      sock.bind(('localhost',6001))
      time.sleep(.300)
      sock.connect(('localhost',6000))
      message1 = 'payload' : 2.1
      sock.send(str(message1).encode('utf8'))
      data = sock.recv(1024)
      var = eval(data.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      time.sleep(.300)
      s.connect(('localhost', 6002))
      s.send(str(message1).encode('utf8'))
      data_recv = sock.recv(1024)
      var = eval(data_recv.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      self.print_events(buffer)
      self.validate(buffer, ack_buf)

      # sends acknowlegment to all the processes in the distributed system.
      def send_ack(self, event, socket):
      if event>0 and socket is not None:
      socket.send(str('ack':event).encode('utf8'))

      # prints the events in the sorted order
      def print_events(self, buffer):
      sorted(buffer)
      for x in range(len(buffer)):
      m = buffer[x]
      frac, whole = math.modf(m)
      each_event = int(frac*10)
      print('Event %d of process %d is happening according to the received order'%(each_event, whole))

      # validates if the msgs sent are received by the processes
      def validate(self, list_of_events, recv_buf):
      count = 0
      if len(list_of_events) > 0 and len(recv_buf) > 0:
      for x in range(len(list_of_events)):
      if list_of_events[x] in recv_buf:
      count += 1
      if count == 2:
      print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

      if __name__ == "__main__":
      p2 = process2()
      time.sleep(.300)
      threading.Thread(target=p2.send_message).start()


      process3.py



      import socket
      import math
      import time
      import threading

      class process3:

      # send message to p2 and p3.
      def send_message(self):
      buffer = [1.1]
      ack_buf =
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      sock.setblocking(True)
      sock.bind(('localhost',6002))
      sock.connect(('localhost',6000))
      message1 = 'payload' : 3.1
      sock.send(str(message1).encode('utf8'))
      data = sock.recv(1024)
      var = eval(data.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], sock)).start()
      elif var.has_key('ack'):
      ack_buf.append(var['ack'])
      s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
      time.sleep(.300)
      s.connect(('localhost', 6001))
      s.send(str(message1).encode('utf8'))
      data_recv = sock.recv(1024)
      var = eval(data_recv.decode('utf8'))
      if 'payload' in var:
      buffer.append(var['payload'])
      threading.Thread(target=self.send_ack, args = (var['payload'], s)).start()
      elif 'ack' in var:
      ack_buf.append(var['ack'])
      self.print_events(buffer)
      self.validate(buffer, ack_buf)

      # sends acknowlegment to all the processes in the distributed system.
      def send_ack(self, event, socket):
      if event>0 and socket is not None:
      socket.send(str('ack':event).encode('utf8'))

      # prints the events in the sorted order
      def print_events(self, buffer):
      sorted(buffer)
      for x in range(len(buffer)):
      m = buffer[x]
      frac, whole = math.modf(m)
      each_event = int(frac*10)
      print('Event %d of process %d is happening according to the received order'%(each_event, whole))

      # validates if the msgs sent are received by the processes
      def validate(self, list_of_events, recv_buf):
      count = 0
      if len(list_of_events) > 0 and len(recv_buf) > 0:
      for x in range(len(list_of_events)):
      if list_of_events[x] in recv_buf:
      count += 1
      if count == 2:
      print("Event %d is sent and acknowledged by the other processes"%(list_of_events[x]))

      if __name__ == "__main__":
      p3 = process3()
      time.sleep(.300)
      threading.Thread(target=p3.send_message).start()


      job.sh



      python3 process1.py &
      python3 process2.py &
      python3 process3.py &







      python sockets clock






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 19:13









      Deekshith BuckyDeekshith Bucky

      215




      215






















          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326455%2fhow-to-run-multiple-replicated-socket-servers-concurrently-in-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326455%2fhow-to-run-multiple-replicated-socket-servers-concurrently-in-python%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3