eXtremeSQL Asynchronous Distributed Connections in Python

The AsyncDistributedConnection class allows applications to execute multiple SQL statements asynchronously across multiple database shards. The method execute() accepts multiple SQL statements and arguments or parameters. An AsyncDistributedConnection instance is created by calling the exdb.connect() method with arguments nodes, async=True and an optional argument nWorkers to specify the number of separate processes to execute the statements. For example:

     
    nodes = ("localhost:5001", "localhost:5002")
     
    conn = self.exdb.connect(nodes=nodes, async=True, nWorkers=2)
         

The connection can now be used to execute single queries which return a single result set , or SQL insert, update or delete statements which return the number of rows affected.

Or the connection can execute multiple statements asynchronously in parallel to produce multiple result sets.

For example, the following code snippet demonstrates using an AsyncDistributedConnection to execute single SQL queries:

     
    nodes = ("localhost:5001", "localhost:5002")
     
    conn = self.exdb.connect(nodes=nodes, async=True, nWorkers=2)
    r = conn.execute("SELECT 1,2")
    print "len(r)=", len(r)
    for k in range(len(r)):
        pprint.pprint(r[k])
 
    r = conn.execute("SELECT 1,2,?", (3, ))
    print "len(r)=", len(r)
    for k in range(len(r)):
        pprint.pprint(r[k])
     

The following code snippet demonstrates using an AsyncDistributedConnection to execute multiple SQL statements asynchronously:

     
    nodes = ("localhost:5001", "localhost:5002")
    nWorkers = 2
     
    conn = self.exdb.connect(nodes=nodes, async=True, nWorkers=nWorkers)
     
    q = ["INSERT INTO MyTable(pk, value, u2, i1, i2, i4, i8, flt, dbl) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"] * nWorkers
 
    args = []
    for i in xrange(nWorkers):
        args.append((i*100, 'str%s' % i, i, i, i, i, i, i*100, i*100.9))
 
    r = conn.execute(q, args)
    print "len(r)=", len(r)
    for k in range(len(r)):
        pprint.pprint(r[k])
     
    q = [
        "SELECT COUNT(*) FROM MyTable",
        "SELECT SUM(pk) FROM MyTable",
        "SELECT SUM(i1) FROM MyTable",
        "SELECT SUM(i2) FROM MyTable"
    ]
 
    r = conn.execute(q, None)
    print "len(r)=", len(r)
    for k in range(len(r)):
        pprint.pprint(r[k])