Advertisement
Uno-Dan

Client

Apr 21st, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. #
  2. from multiprocessing import Process
  3.  
  4. from zmq import Context
  5. from zmq.backend.cython.constants import REQ
  6.  
  7. NBR_CLIENTS = 10
  8.  
  9. FRONTEND_PORT = '7777'
  10. FRONTEND_SERVER = '127.0.0.1'
  11.  
  12. BACKEND_PORT = '7778'
  13. BACKEND_SERVER = '127.0.0.2'
  14.  
  15.  
  16. def client_task(ident):
  17.     """Basic request-reply client using REQ socket."""
  18.     socket = Context().socket(REQ)
  19.  
  20.     identity = f'Client-{ident}'
  21.     socket.identity = identity.encode('utf-8')
  22.     socket.connect(f'tcp://{FRONTEND_SERVER}:{FRONTEND_PORT}')
  23.  
  24.     # Send request, get reply
  25.     socket.send_string('Ping')
  26.     reply = socket.recv_string()
  27.     print(f'{identity}: {reply}')
  28.  
  29.  
  30. def main():
  31.     # Start client tasks
  32.     def start(task, *args):
  33.         process = Process(target=task, args=args)
  34.         process.daemon = True
  35.         process.start()
  36.  
  37.     for idx in range(NBR_CLIENTS):
  38.         start(client_task, idx)
  39.  
  40.     socket = Context().socket(REQ)
  41.     socket.connect(f'tcp://{BACKEND_SERVER}:{BACKEND_PORT}')
  42.     socket.send_string('STOP')
  43.  
  44.     while True:
  45.         pass
  46.  
  47.  
  48. if __name__ == "__main__":
  49.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement