Advertisement
Uno-Dan

client

Apr 22nd, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.32 KB | None | 0 0
  1. #
  2. from multiprocessing import Process
  3.  
  4. from zmq import Context
  5. from zmq.backend.cython.constants import REQ, NOBLOCK
  6.  
  7. NBR_CLIENTS = 10
  8.  
  9. FRONTEND_PORT = '7770'
  10. FRONTEND_SERVER = '127.0.0.1'
  11.  
  12. BACKEND_PORT = '7771'
  13. BACKEND_SERVER = '127.0.0.2'
  14.  
  15.  
  16. class App:
  17.     def __init__(self):
  18.         self.processes = []
  19.  
  20.     def start(self, nbr_clients):
  21.         for idx in range(nbr_clients):
  22.             self.start_processes(self.client_task, idx)
  23.  
  24.         for process in self.processes:
  25.             process.join()
  26.  
  27.     @staticmethod
  28.     def client_task(index):
  29.         socket = Context().socket(REQ)
  30.  
  31.         identity = f'Client-{index}'
  32.         socket.identity = identity.encode('utf-8')
  33.         socket.connect(f'tcp://{FRONTEND_SERVER}:{FRONTEND_PORT}')
  34.  
  35.         socket.send_string('Ping')
  36.         reply = socket.recv_string()
  37.         print(f'{identity}: {reply}')
  38.  
  39.     def start_processes(self, task, *args):
  40.         process = Process(target=task, args=args)
  41.         self.processes.append(process)
  42.         process.daemon = True
  43.         process.start()
  44.  
  45.  
  46. def main():
  47.     app = App()
  48.     app.start(NBR_CLIENTS)
  49.  
  50.     ctx = Context()
  51.     socket = ctx.socket(REQ)
  52.     socket.connect(f'tcp://{BACKEND_SERVER}:{BACKEND_PORT}')
  53.     socket.send_string('STOP', NOBLOCK)
  54.  
  55.  
  56. if __name__ == "__main__":
  57.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement