Simulating multi-user to operate PostgreSQL by Pythonï¼
<p>I want to compare my distributed database and postgresql. So I need to simulate multi-user SQL operations on postgresql using Python.</p>

<p>Can I use the <code>mutliprocessing</code> moduleï¼</p>

<p>here is my code</p>

<pre><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) &amp;&amp; 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()
</code></pre>

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

<p>You can use the <code>random</code> 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.</p>

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

python postgresql parallel-processing