diff --git a/examples/Python/mtrelay.py b/examples/Python/mtrelay.py index e22d1a9a..35ed8ea6 100644 --- a/examples/Python/mtrelay.py +++ b/examples/Python/mtrelay.py @@ -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 @@ -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 diff --git a/examples/Python/mtserver.py b/examples/Python/mtserver.py index cbee5734..ce1b75a5 100644 --- a/examples/Python/mtserver.py +++ b/examples/Python/mtserver.py @@ -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") diff --git a/examples/Python/psenvpub.py b/examples/Python/psenvpub.py index 005bd8ce..e0ca1615 100644 --- a/examples/Python/psenvpub.py +++ b/examples/Python/psenvpub.py @@ -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") diff --git a/examples/Python/psenvsub.py b/examples/Python/psenvsub.py index 358d89dc..5d0f010c 100644 --- a/examples/Python/psenvsub.py +++ b/examples/Python/psenvsub.py @@ -7,11 +7,12 @@ """ 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") @@ -19,7 +20,7 @@ def main(): 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() diff --git a/examples/Python/rrclient.py b/examples/Python/rrclient.py index 78d662e2..faa20be8 100644 --- a/examples/Python/rrclient.py +++ b/examples/Python/rrclient.py @@ -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}]") diff --git a/examples/Python/rrworker.py b/examples/Python/rrworker.py index 9dd47e12..7c29f946 100644 --- a/examples/Python/rrworker.py +++ b/examples/Python/rrworker.py @@ -11,5 +11,5 @@ while True: message = socket.recv() - print("Received request: %s" % message) + print(f"Received request: {message}") socket.send(b"World") diff --git a/examples/Python/syncpub.py b/examples/Python/syncpub.py index 2f7805c4..8d1571dc 100644 --- a/examples/Python/syncpub.py +++ b/examples/Python/syncpub.py @@ -6,6 +6,7 @@ # We wait for 10 subscribers SUBSCRIBERS_EXPECTED = 10 + def main(): context = zmq.Context() @@ -13,11 +14,11 @@ def main(): 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 @@ -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() diff --git a/examples/Python/syncsub.py b/examples/Python/syncsub.py index 0494fa95..babf806e 100644 --- a/examples/Python/syncsub.py +++ b/examples/Python/syncsub.py @@ -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'') @@ -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()