Skip to content

Commit

Permalink
Merge pull request #841 from pktl/update-examples-chapter2
Browse files Browse the repository at this point in the history
Update Python examples in Chapter 2
  • Loading branch information
sappo authored Jun 16, 2021
2 parents 444df88 + 61f685e commit 18034c6
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 28 deletions.
5 changes: 3 additions & 2 deletions examples/Python/mtrelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import threading
import zmq

def step1(context=None):

def step1(context: zmq.Context = None):
"""Step 1"""
context = context or zmq.Context.instance()
# Signal downstream to step 2
Expand All @@ -19,7 +20,7 @@ def step1(context=None):
sender.send(b"")


def step2(context=None):
def step2(context: zmq.Context = None):
"""Step 2"""
context = context or zmq.Context.instance()
# Bind to inproc: endpoint, then start upstream thread
Expand Down
15 changes: 7 additions & 8 deletions examples/Python/mtserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@
import zmq


def worker_routine(worker_url, context=None):
def worker_routine(worker_url: str,
context: zmq.Context = None):
"""Worker routine"""
context = context or zmq.Context.instance()

# Socket to talk to dispatcher
socket = context.socket(zmq.REP)

socket.connect(worker_url)

while True:
string = socket.recv()
print(f"Received request: [ {string} ]")

string = socket.recv()

print("Received request: [ %s ]" % (string))

# do some 'work'
# Do some 'work'
time.sleep(1)

#send reply back to client
# Send reply back to client
socket.send(b"World")


Expand Down
3 changes: 2 additions & 1 deletion examples/Python/psenvpub.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import time
import zmq


def main():
"""main method"""

# Prepare our context and publisher
context = zmq.Context()
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.bind("tcp://*:5563")

Expand Down
5 changes: 3 additions & 2 deletions examples/Python/psenvsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
"""
import zmq


def main():
""" main method """

# Prepare our context and publisher
context = zmq.Context()
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.connect("tcp://localhost:5563")
subscriber.setsockopt(zmq.SUBSCRIBE, b"B")

while True:
# Read envelope with address
[address, contents] = subscriber.recv_multipart()
print("[%s] %s" % (address, contents))
print(f"[{address}] {contents}")

# We never get here but clean up anyhow
subscriber.close()
Expand Down
4 changes: 2 additions & 2 deletions examples/Python/rrclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
socket.connect("tcp://localhost:5559")

# Do 10 requests, waiting each time for a response
for request in range(1,11):
for request in range(1, 11):
socket.send(b"Hello")
message = socket.recv()
print("Received reply %s [%s]" % (request, message))
print(f"Received reply {request} [{message}]")
2 changes: 1 addition & 1 deletion examples/Python/rrworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@

while True:
message = socket.recv()
print("Received request: %s" % message)
print(f"Received request: {message}")
socket.send(b"World")
14 changes: 8 additions & 6 deletions examples/Python/syncpub.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
# We wait for 10 subscribers
SUBSCRIBERS_EXPECTED = 10


def main():
context = zmq.Context()

# Socket to talk to clients
publisher = context.socket(zmq.PUB)
# set SNDHWM, so we don't drop messages for slow subscribers
publisher.sndhwm = 1100000
publisher.bind('tcp://*:5561')
publisher.bind("tcp://*:5561")

# Socket to receive signals
syncservice = context.socket(zmq.REP)
syncservice.bind('tcp://*:5562')
syncservice.bind("tcp://*:5562")

# Get synchronization from subscribers
subscribers = 0
Expand All @@ -27,13 +28,14 @@ def main():
# send synchronization reply
syncservice.send(b'')
subscribers += 1
print("+1 subscriber (%i/%i)" % (subscribers, SUBSCRIBERS_EXPECTED))
print(f"+1 subscriber ({subscribers}/{SUBSCRIBERS_EXPECTED})")

# Now broadcast exactly 1M updates followed by END
for i in range(1000000):
publisher.send(b'Rhubarb')
publisher.send(b"Rhubarb")

publisher.send(b"END")

publisher.send(b'END')

if __name__ == '__main__':
if __name__ == "__main__":
main()
13 changes: 7 additions & 6 deletions examples/Python/syncsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
# Synchronized subscriber
#
import time

import zmq


def main():
context = zmq.Context()

# First, connect our subscriber socket
subscriber = context.socket(zmq.SUB)
subscriber.connect('tcp://localhost:5561')
subscriber.connect("tcp://localhost:5561")
subscriber.setsockopt(zmq.SUBSCRIBE, b'')

time.sleep(1)

# Second, synchronize with publisher
syncclient = context.socket(zmq.REQ)
syncclient.connect('tcp://localhost:5562')
syncclient.connect("tcp://localhost:5562")

# send a synchronization request
syncclient.send(b'')
Expand All @@ -29,11 +29,12 @@ def main():
nbr = 0
while True:
msg = subscriber.recv()
if msg == b'END':
if msg == b"END":
break
nbr += 1

print ('Received %d updates' % nbr)
print(f"Received {nbr} updates")


if __name__ == '__main__':
if __name__ == "__main__":
main()

0 comments on commit 18034c6

Please sign in to comment.