Python client and examples

Python client is updated and also available on pip for your convenience! It also comes with a neat PUB-SUB example that uses our publicly accessible server,

There is documentation for python client available here. We’ll also go over the code here for a quick reference.

First, get the client:

$ pip install pubsubsql

To set up a subscriber running in a matter of seconds, just connect, subscribe and wait for data to arrive:

import time
import random

import pubsubsql as pss

subscriber = pss.Client()


subscriber.execute("subscribe * from Stocks where MarketCap = 'MEGA CAP'")

timeout = 60000

while subscriber.waitForPubSub(timeout):
    print "*********************************"
    print "Action:{}".format(subscriber.getAction())
    while subscriber.nextRow():
        for ordinal, column in enumerate(subscriber.getColumns()):
            print "{}:{}".format(column, subscriber.getValue(ordinal)),
        print ""

And that’s it! Just build it and run it!

Running the subscriber on public server

In case you don’t see any data, no one is publishing it. In that case, you need to build the publisher as well. This publisher will randomly update the price of Google stock:

import time
import random

import pubsubsql as pss

client = pss.Client()


    client.execute("key Stocks Ticker")
    client.execute("tag Stocks MarketCap")
    # key or tag may have already been defined, so its ok

    client.execute("insert into Stocks (Ticker, Price, MarketCap) values (GOOG, '1,2002d.22', 'MEGA CAP')")

while 1:
    command = "update Stocks set Price='%f' where Ticker='GOOG'" % (10000.0 * random.uniform(0.9, 1.9))

If you have any issues, just log an issue to github project or send us an email!

Share this post

comments powered by Disqus