Tuesday, August 16, 2016

pyzmq 15.4 and python 3.5.2

It takes a little time to get the latest pyzmq installed.  So far, here is a list of things that I have done.
Note: This is on Ubuntu 16.04.

Preparation
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
sudo python3 -m pip install cython

Download zeromq 4.1.5
http://zeromq.org/intro:get-the-software

Build zeromq 4.1.5
tar xf zeromq-4.1.5.tar.gzcd zeromq-4.1.5
./configure
make
sudo make install

Download pyzmq 15.4
https://github.com/zeromq/pyzmq

unzip pyzmq.zip

python3 setup.py configure --zmq=/usr/local

sudo python3 setup.py install

===============================
pub sub sample

python3 topics_sub.py "tcp://127.0.0.1:5556"
python3 topics_pub.py "tcp://127.0.0.1:5556" ""
===============================
topics_pub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.

Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost.  More than
one subscriber can listen, and they can listen to  different topics.

Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks'  while listening to 'w' is enough to
catch 'weather'.
"""

#-----------------------------------------------------------------------------
#  Copyright (c) 2010 Brian Granger
#
#  Distributed under the terms of the New BSD License.  The full license is in
#  the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------

import itertools
import sys
import time

import zmq

def main():
    if len (sys.argv) != 2:
        print('usage: publisher <bind-to>')
        sys.exit (1)

    bind_to = sys.argv[1]

    all_topics = ['sports.general','sports.football','sports.basketball',
                  'stocks.general','stocks.GOOG','stocks.AAPL',
                  'weather']

    ctx = zmq.Context()
    s = ctx.socket(zmq.PUB)
    s.bind(bind_to)

    print("Starting broadcast on topics:")
    print("   %s" % all_topics)
    print("Hit Ctrl-C to stop broadcasting.")
    print("Waiting so subscriber sockets can connect...")
    print()
    time.sleep(1.0)

    msg_counter = itertools.count()
    try:
        for topic in itertools.cycle(all_topics):
            msg_body = str(next(msg_counter))
            print("    Topic: {}, msg:{}".format(topic, msg_body))
            s.send_multipart([topic.encode(), msg_body.encode()])
            # short wait so we don't hog the cpu
            time.sleep(0.1)
    except KeyboardInterrupt:
        pass

    print("Waiting for message queues to flush...")
    time.sleep(0.5)
    print("Done.")

if __name__ == "__main__":
    main()


topics_sub.py
#!/usr/bin/env python
"""Simple example of publish/subscribe illustrating topics.

Publisher and subscriber can be started in any order, though if publisher
starts first, any messages sent before subscriber starts are lost.  More than
one subscriber can listen, and they can listen to  different topics.

Topic filtering is done simply on the start of the string, e.g. listening to
's' will catch 'sports...' and 'stocks'  while listening to 'w' is enough to
catch 'weather'.
"""

#-----------------------------------------------------------------------------
#  Copyright (c) 2010 Brian Granger, Fernando Perez
#
#  Distributed under the terms of the New BSD License.  The full license is in
#  the file COPYING.BSD, distributed as part of this software.
#-----------------------------------------------------------------------------

import sys
import time

import zmq
import numpy

def main():
    if len (sys.argv) < 2:
        print ('usage: subscriber <connect_to> [topic topic ...]')
        sys.exit (1)

    connect_to = sys.argv[1]
    topics = sys.argv[2:]

    ctx = zmq.Context()
    s = ctx.socket(zmq.SUB)
    print(connect_to)
    s.connect(connect_to)

    # manage subscriptions
    if not topics:
        print("Receiving messages on ALL topics...")
        s.setsockopt(zmq.SUBSCRIBE,'')
    else:
        print("Receiving messages on topics: %s ..." % topics)
        for t in topics:
            s.setsockopt_string(zmq.SUBSCRIBE,t)
    print()
    try:
        while True:
            topic, msg = s.recv_multipart()
            print('   Topic: %s, msg:%s' % (topic, msg))
    except KeyboardInterrupt:
        pass
    print("Done.")

if __name__ == "__main__":
    main()




No comments:

Post a Comment