I have query which I would like to submit in parallel using the multiprocessing.Pool module.
My question is how I can submit the query along with the corr. binding variables to pool.map
?
So if the query is something like:
SELECT a.col1, a.col2, t.col1 FROM table a,
(VALUES(:x1,:x2),(:y1,:y2),(:z1,:z2))
AS t(col1,col2) WHERE a.col2=col2 AND a.col1 % t.col1
and I dict of binding variables with each query:
{x1 : 'AA', x2:'XX',y1:'BB', y2:'YY',z1:'CC',z2:'ZZ'}
How can I invoke pool.map(..)
on this ?
I ask this because pool.map()
only supports one-argument worker functions
Now, if I WASN'T using binding variables, I would invoke it like this:
def execSQL():
#init db_user, db_host, sql_qry here
...
conn = psycopg2.connect("dbname=%s host=%s user=%s password=%s" % (db_name, db_host, db_user, db_pwd))
curs = conn.cursor()
curs.execute(sql_qry)
records = curs.fetchall()
return records
def run():
sql_queries = [...] # some list of queries to be run in parallel
pool = Pool(processes=4)
results=pool.map(execSQL, sql_queries)