SAM pools and multi-threading

A SAM pool enables a single set of credentials to be shared between client side threads to leverage multiple threads of parallelism on the 1010data Insights Platform.

Creating a new Session object for a SAM pool user is similar to creating a single-user Session object.

testSession=py1010.Session(gateway, owner, pwd, py1010.POSSESS, group)

The SAM pool user session takes two different parameters. Instead of the individual user name, this Session object takes the owner parameter. The owner parameter is the group owner's user name. The group parameter is the group ID. The SAM pool Session constructor takes the following parameter variables:

If your users are using a SAM pool to log in, they will not have their own platform IDs. The users share a single set of credentials to log into the platform. A SAM pool has a number of IDs to use, but it is not limitless. You may have to account for cases where peak utilization creates a situation where more threads are trying to access the platform than your SAM pool is configured to handle.

The Python SDK is designed to queue during Session object construction when a SAM pool is fully utilized. During this queuing time the thread trying to construct a new Session object retries the request every ten seconds.

To ensure that your SAM pool is fully utilized, clean up sessions as soon as your thread is done to prevent a full pool of IDs where all of the threads of database concurrency are in use at the same time. For more information, see Cleaning up a session.

In this example, two threads construct two separate Session objects and log in with a SAM pool. The application retrieves data from two different tables and downloads them into two separate files.

import py1010
import threading
import sys

gateway = "https://www2.1010data.com/cgi-bin/gw"
String group = "[GROUP_ID]"
String owner = "[GROUP_OWNER_USERNAME]"
String pwd = "[GROUP_OWNER_PASSWORD]"
tablename = "pub.demo.weather.stations"

def runQuery(table, ops, outfilename):
    try:
        with py1010.Session(gateway, owner, pwd, py1010.POSSESS,
                            group) as session:
            print("Logged in as {}".format(session.username))
            query = session.query(table, ops)
            query.run()
            with file(outfilename, "w") as outfile:
                print("Starting to write to " + outfilename)
                for r in query.rows:
                    outfile.write("\t".join([str(x) for x in r]) + "\n")
    except py1010.TentenException as e:
        print("Error: {}".format(e))

t1=threading.Thread(target=runQuery,
                    args=("pub.public_data.census.acs.all_acs_processed_tables",
                          '<sel value="(i_<100000)"/>', "Output1.txt"))
t2=threading.Thread(target=runQuery,
                    args=("pub.public_data.census.acs.reference.column_names",
                          '<sel value="(i_<100000)"/>', "Output2.txt"))
t1.start()
t2.start()

t1.join()
t2.join()
Note: [GROUP_ID], [GROUP_OWNER_USERNAME], and [GROUP_OWNER_PASSWORD] are placeholders for valid Insights Platform group ID, owner name, and password.