MySQL select request in parallel (python)

三世轮回 提交于 2020-02-03 02:08:32

问题


I saw a "similar" post Executing MySQL SELECT * query in parallel, buy my question is different, and this has not been answered either, so i guess its not a duplicate.

I am trying to do a MySQL select request in parallel. The reason is because i need the response fast. I managed to create the request when i paralleled the connection as well, but as the connection takes more time then the actual select it would be faster to connect one time, and do the select in parallel.

My approach:

import mysql.connector
import multiprocessing

def func(id):
    return GetFasta(id, cnx).sequence()

cnx = mysql.connector.connect(user='U_NAME', host='IP_ADDR', database='DB_NAME')
p = multiprocessing.Pool()

for x in p.imap_unordered(func, ["Q32P44", "ASD"]):
    print x

p.close()
p.join()
cnx.close()

The GetFasta is a Class and its working as intended, requires a connector, and an ID and does a simple SELECT with those.

If i execute this script the interpreter freezes, and i have to kill it manually. I guess it is either not this trivial, or maybe impossible to select in parallel with one connector.

Is it?

Any workaround?


回答1:


I found the "solution" after a lot of research. It is actually not possible. MySQL does not support parallel queries from the same user connection. It is absolutely impossible, and there are no plans to add this.

The best you can get is the way i implemented it, so parallel the connection as well.

In some other types of databases it is possible, but for now i ll stick with MySQL and use the parallel connection method



来源:https://stackoverflow.com/questions/35040848/mysql-select-request-in-parallel-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!