zeromq samples getting the message out
2015-04-11 16:40
85 查看
Getting the Message Out
The second classic pattern is one-way data distribution,in which a server pushes
updates to a set of a set of clients.Let's see an example that pushes out weather
updates consisting of a zip code,temperature,and relative humidity.
![](http://img.blog.csdn.net/20150411171050648?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTc2NzYxMQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Here's the server.We'll use port 5556 for this application:
wuserver: Weather update server in Python
There's no start and no end to this stream of updates,it's like a never ending broadcast.
Here is the client application,which listens to the stream of updates and grabs anything
to do with a specified zip code,by default New York City because that's a great place to
start any adventure:
wuserver: Weather update client in Python
The second classic pattern is one-way data distribution,in which a server pushes
updates to a set of a set of clients.Let's see an example that pushes out weather
updates consisting of a zip code,temperature,and relative humidity.
Here's the server.We'll use port 5556 for this application:
wuserver: Weather update server in Python
#!/usr/bin/python # # Weather update server # Binds PUB socket to tcp://*5556 # Publishes random weather updates # import zmq from random import randrange context = zmq.Context() socket = context.socket(zmq.PUB) socket.bind("tcp://*:5556") while True: zipcode = randrange(1,100000) temperature = randrange(-80,135) relhumidity = randrange(10,60) socket.send_string("%i %i %i" % (zipcode,temperature,relhumidity))
There's no start and no end to this stream of updates,it's like a never ending broadcast.
Here is the client application,which listens to the stream of updates and grabs anything
to do with a specified zip code,by default New York City because that's a great place to
start any adventure:
wuserver: Weather update client in Python
#!/usr/bin/python # # Weather update client # Connects SUB socket to tcp://localhost:5556 # Collects weather updates and finds avg temp in zipcode # import sys import zmq # Socket to talk to server context = zmq.Context() socket = context.socket(zmq.SUB) print("Collecting updates from weather server...") socket.connect("tcp://localhost:5556") #Subscribe to zipcode,default is NYC,10001 zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001" #Python 2 - ascii bytes to unicode str if isinstance(zip_filter,bytes): zip_filter = zip_filter.decode('ascii') socket.setsockopt_string(zmq.SUBSCRIBE,zip_filter) #Process 5 updates total_temp = 0
<span style="font-family: Arial, Helvetica, sans-serif;">for update_nbr in range(5):</span>
string = socket.recv_string() zipcode,temperature,relhumidity = string.split() total_temp += int(temperature) print("Average temperature for zipcode '%s' was %dF" %( zip_filter,total_temp / update_nbr) )
相关文章推荐
- Getting the most out of IDispatch(XYDispDriver)
- XMLBeans: BEA gets the message out
- Getting to know ITSmobile by Checking out the Internet Services
- 3 Getting the Most Out of Everyday Tools
- [Graphics] UIColor created with component values far outside the expected range, Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
- Getting the most out of your pixels - adapting to view state changes(WIN8下设计适应多种分辨率的APP)[转.原]
- publishing failed with multiple errors resource is out of sync with the file system
- Scale-out of the Performance of SignalR
- Resource is out of sync with the file system的解决办法
- 解决eclipse中出现Resource is out of sync with the file system问题
- The source attachment does not contain the source for the file MessageQueue.class
- Passing the Message 单调递减栈
- 异常处理:Sys.WebForms.PageRequestManagerParserErrorException:The message……
- Teddy's Aspect Weaver Version 0.3 with Great Updating and Fixing, Especially the Implementing of Getting Runtime Method Context Info and Method Arguments in MSIL Level
- Installing Windows Identity Foundation on Windows 8 - The Certificate for the signer of the message is invalid or not found.
- print out the path from one node to another in binary tree
- The MD5 Message-Digest Algorithm
- Failed to load resource: the server responded with a status of 504 (Gateway Time-out)
- WPF 4.5 is here : check out the new features !