通过pymysql用多进程更新一个mysql表

rmbxnbpk  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(477)

实际上,我正在尝试通过 pymysql ,每个进程从一个巨大的csv文件中读取一个csv文件以提高速度。但我知道 Lock wait timeout exceeded; try restarting transaction exception 当我运行脚本时。在搜索了这个站点上的帖子之后,我发现了一个帖子,其中提到要设置或构建内置的load\u data\u infle,但没有关于它的详细信息。如何使用“pymysql”实现我的目标?

xesrikrc

xesrikrc1#

-------第一次编辑

qxsslcnc

qxsslcnc2#

您的问题是,您超过了从服务器获取响应所允许的时间,因此客户端将自动超时。根据我的经验,将等待超时时间调整到6000秒左右,合并成一个csv,只留下数据导入。另外,我建议直接从mysql而不是python运行查询。
我通常将csv数据从python导入mysql的方法是通过insert。。。值。。。方法,我只在需要对数据进行某种操作(即将不同的行插入不同的表)时才这样做。
我喜欢你的方法,理解你的想法,但实际上没有必要。插入的好处。。。值。。。方法是您不会遇到任何超时问题。

cxfofazt

cxfofazt3#

以下是作业方法:

`def importprogram(path, name):
    begin = time.time()
    print('begin to import program' + name + ' info.')
    # "c:\\sometest.csv"
    file = open(path, mode='rb')
    csvfile = csv.reader(codecs.iterdecode(file, 'utf-8'))

    connection = None
    try:
        connection = pymysql.connect(host='a host', user='someuser', password='somepsd', db='mydb',
                                 cursorclass=pymysql.cursors.DictCursor)
        count = 1
        with connection.cursor() as cursor:
            sql = '''update sometable set Acolumn='{guid}' where someid='{pid}';'''
            next(csvfile, None)
            for line in csvfile:
                try:
                    count = count + 1
                    if ''.join(line).strip():
                        command = sql.format(guid=line[2], pid=line[1])
                        cursor.execute(command)
                    if count % 1000 == 0:
                        print('program' + name + ' cursor execute', count)
                except csv.Error:
                    print('program csv.Error:', count)
                    continue
                except IndexError:
                    print('program IndexError:', count)
                    continue
                except StopIteration:
                    break
    except Exception as e:
        print('program' + name, str(e))
    finally:
        connection.commit()
        connection.close()
        file.close()
    print('program' + name + ' info done.time cost:', time.time()-begin)`

以及多重处理方法:

import multiprocessing as mp
def multiproccess():
    pool = mp.Pool(3)
    results = []
    paths = ['C:\\testfile01.csv', 'C:\\testfile02.csv', 'C:\\testfile03.csv']
    name = 1
    for path in paths:
        results.append(pool.apply_async(importprogram, args=(path, str(name))))
        name = name + 1

    print(result.get() for result in results)
    pool.close()
    pool.join()

主要方法是:

if __name__ == '__main__':
    multiproccess()

我对Python还不熟悉。我怎样才能使代码或方式本身出错?我应该只用一个进程来完成数据的读取和导入吗?

相关问题