Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- import time
- import zmq
- from tqdm import trange
- import json
- ```
- Server is REQ. Client is REP.
- ```
- # create two connecting clients
- context = zmq.Context()
- socket = context.socket(zmq.REP)
- socket.connect("tcp://localhost:5555")
- count = 0
- while True:
- message = socket.recv()
- socket.send(b"")
- count += 1
- print(json.loads(message.decode())['n'])
- print(count)
- ```
- ```
- # create one binding server
- context = zmq.Context()
- socket = context.socket(zmq.REQ)
- socket.bind("tcp://*:5555")
- for n in trange(100):
- message = json.dumps({"n": n}).encode()
- socket.send(message)
- socket.recv()
- time.sleep(1)
- ```
- Output:
- ```
- $$ python responder.py
- 2
- 4
- 6
- 8
- 10
- 12
- <<< fall back full on this responder
- 14
- 15
- 16
- 17
- 18
- 19
- $$ python responder.py
- 0
- 1
- 3
- 5
- 7
- 9
- 11
- 13
- ^CTraceback (most recent call last):
- File "responder.py", line 17, in <module>
- message = socket.recv()
- File "zmq/backend/cython/socket.pyx", line 791, in zmq.backend.cython.socket.Socket.recv
- File "zmq/backend/cython/socket.pyx", line 827, in zmq.backend.cython.socket.Socket.recv
- File "zmq/backend/cython/socket.pyx", line 186, in zmq.backend.cython.socket._recv_copy
- File "zmq/backend/cython/checkrc.pxd", line 13, in zmq.backend.cython.checkrc._check_rc
- KeyboardInterrupt
- ```
- Server is REP. Client is REQ
- ```
- # create two connecting clients
- context = zmq.Context()
- socket = context.socket(zmq.REP)
- socket.bind("tcp://*:5555")
- count = 0
- while True:
- message = socket.recv()
- socket.send(b"")
- count += 1
- print(json.loads(message.decode())['n'])
- print(count)
- ```
- ```
- # create one binding server
- context = zmq.Context()
- socket = context.socket(zmq.REQ)
- socket.connect("tcp://localhost:5555")
- socket.connect("tcp://localhost2:5555")
- for n in trange(100):
- message = json.dumps({"n": n}).encode()
- socket.send(message)
- socket.recv()
- time.sleep(1)
- ```
- Output:
- ```
- $$ python responder.py
- 0
- <<< stuck
- ```
Advertisement
Add Comment
Please, Sign In to add comment