python mysql 并行,并行MySQL选择请求(python)

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?

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值