python2.7中的pool.apply\u async不会将值插入MySQL表
我正在尝试运行以下代码,为某个应用程序并行填充一个表。首先定义下面的函数,该函数应该连接到MyDB,并使用给定的值执行sql命令以插入表中python2.7中的pool.apply\u async不会将值插入MySQL表,mysql,google-cloud-platform,python-multithreading,executemany,python-dedupe,Mysql,Google Cloud Platform,Python Multithreading,Executemany,Python Dedupe,我正在尝试运行以下代码,为某个应用程序并行填充一个表。首先定义下面的函数,该函数应该连接到MyDB,并使用给定的值执行sql命令以插入表中 def dbWriter(sql, rows) : # load cnf file MYSQL_CNF = os.path.abspath('.') + '/mysql.cnf' conn = MySQLdb.connect(db='dedupe', charset='utf8',
def dbWriter(sql, rows) :
# load cnf file
MYSQL_CNF = os.path.abspath('.') + '/mysql.cnf'
conn = MySQLdb.connect(db='dedupe',
charset='utf8',
read_default_file = MYSQL_CNF)
cursor = conn.cursor()
cursor.executemany(sql, rows)
conn.commit()
cursor.close()
conn.close()
然后是这一段:
pool = dedupe.backport.Pool(processes=2)
done = False
while not done :
chunks = (list(itertools.islice(b_data, step)) for step in
[step_size]*100)
results = []
for chunk in chunks :
print len(chunk)
results.append(pool.apply_async(dbWriter,
("INSERT INTO blocking_map VALUES (%s, %s)",
chunk)))
for r in results :
r.wait()
if len(chunk) < step_size :
done = True
pool.close()
一切正常,没有错误。但最后,我的表是空的,这意味着不知何故插入没有成功。我已经尝试了很多方法来解决这个问题,包括在多次谷歌搜索后添加列名以供插入,但都没有成功。如有任何建议,将不胜感激。在python2.7和gcloud ubuntu中运行代码。请注意,粘贴到此处后,缩进可能会有点混乱
还请注意,chunk完全遵循所需的数据格式
注意。这是其中的一部分
请注意,在上面链接的示例中,我唯一要更改的是,我正在分离创建和插入表的步骤,因为我在gcloud平台上运行代码,并且它强制执行GTID标准。解决方案将dbwriter函数更改为:
conn = MySQLdb.connect(host = # host ip,
user = # username,
passwd = # password,
db = 'dedupe')
cursor = conn.cursor()
cursor.executemany(sql, rows)
cursor.close()
conn.commit()
conn.close()
事实证明,问题在于使用MYSQL_CNF=os.path.abspath'.+'/MYSQL.CNF'。我手动插入了连接参数,现在可以工作了。您可以添加解决方案作为答案吗?谢谢。你能具体说明一下MySQL在哪里运行,代码运行控制台VM实例在哪里吗?