0

I want to compare my distributed database and postgresql. So I need to simulate multi-user SQL operations on postgresql using Python.

Can I use the mutliprocessing module?

here is my code

 # encoding=utf-8
import datetime
import multiprocessing
import psycopg2

def exe(cmd):
    conn = psycopg2.connect("dbname = test user = pj password = dbrgdbrg")
    cur = conn.cursor()
    try:
        sql = "SELECT id FROM test WHERE ST_MAKEENVELOPE(118,38,119,39,4326) && wkb_geometry;"
        cur.execute(sql)
        print cur.fetchone()
    except Exception, e:
        print e

if __name__ == "__main__":
    cmds=range(5)
    for cmd in  cmds:
        p = multiprocessing.Process(target=exe,args=(cmd,))
        p.start()
        p.join()

But I don't know if it is correct? What should I do if I wanted to create random parameter to my SQL statements?

1 Answer 1

1

You can use the random function to generate random 'ST_MAKEENVELOPE` qualifiers this is assuming they belong to some range. And then use the random function to get 5 different ID's of ST_MAKEENVELOPE.

Another thing you can just use for cmd in range(5): instead of creating a list and then using it.

Sign up to request clarification or add additional context in comments.

2 Comments

but when I running sql = "SELECT id from test where id < random.randint(0,10) " it is prompt psycopg2.ProgrammingError: schema "random" does not exist LINE 1: SELECT id from test where id < random.randint(0,10) ^
sql = "SELECT id from test where id < " + str(random.randint(0,10))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.