0

I've the following list of queries:

queries = ["SELECT * FROM db.trans", "SELECT * FROM db.order", "SELECT * FROM db.account UNION ALL SELECT * FROM db.account2", "SELECT * FROM db.trans UNION ALL SELECT * FROM db.trans2"]

And using my database connection I am trying to run all this queries in parallel instead of:

for query in queries:
  cursor.execute(query)

I'm trying to use multiprocessing library and my current code is:

import multiprocessing
def work():
   for query in queries:
      cursor.execute(query)

if __name__ == '__main__':
    _p = multiprocessing.Process(target=work)
    _p.start()
    _p.join()

However, it is taking the same time as the sequential mode... anyone knows how to run a parallel processing?

Thanks!

Pedro Alves
  • 1,004
  • 1
  • 21
  • 47
  • You might find that multithreading would be the more efficient approach over multiprocessing. – Booboo Jan 14 '21 at 20:00

1 Answers1

0

It could be something like this:

if __name__ == '__main__':
    _processes = []
    for query in queries:
        _p = multiprocessing.Process(target=cursor.execute, args=(query,))
        _p.start()
        _processes.append(_p)

    for _p in _processes:
        _p.join()

Arseniy
  • 680
  • 5
  • 10