Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Python examples in Chapter 2 #841

Merged
merged 5 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()