-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreqrep.py
More file actions
49 lines (40 loc) · 1.1 KB
/
reqrep.py
File metadata and controls
49 lines (40 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import trio
import trzmq
import zmq
async def req():
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.SNDHWM, 2)
socket.connect("tcp://0.0.0.0:5556")
s = trzmq.Socket(socket)
i = 1
while True:
topic = b"ZMQ-Test"
message = f"Hello {i}".encode()
await s.send(b"%b %b" % (topic, message))
print("Sent: %s %s" % (topic, message))
reply = await s.recv()
print("Got response: %s" % reply)
i += 1
await trio.sleep(1)
async def rep():
await trio.sleep(10)
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.setsockopt(zmq.SNDHWM, 2)
socket.bind("tcp://0.0.0.0:5556")
s = trzmq.Socket(socket)
i = 1
while True:
msg = await s.recv()
await s.send(b'Reply to: ' + msg)
async def printer():
while True:
print("Still alive")
await trio.sleep(1)
async def run():
async with trio.open_nursery() as nursery:
nursery.start_soon(req)
nursery.start_soon(rep)
nursery.start_soon(printer)
trio.run(run)