`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()
3条答案
按热度按时间xesrikrc1#
-------第一次编辑
qxsslcnc2#
您的问题是,您超过了从服务器获取响应所允许的时间,因此客户端将自动超时。根据我的经验,将等待超时时间调整到6000秒左右,合并成一个csv,只留下数据导入。另外,我建议直接从mysql而不是python运行查询。
我通常将csv数据从python导入mysql的方法是通过insert。。。值。。。方法,我只在需要对数据进行某种操作(即将不同的行插入不同的表)时才这样做。
我喜欢你的方法,理解你的想法,但实际上没有必要。插入的好处。。。值。。。方法是您不会遇到任何超时问题。
cxfofazt3#
以下是作业方法:
以及多重处理方法:
主要方法是:
我对Python还不熟悉。我怎样才能使代码或方式本身出错?我应该只用一个进程来完成数据的读取和导入吗?