[英]How to insert async into MySQL using Python
我想在MySQL數據庫中插入很多數據。 我使用gevent
從Internet下載數據,然后將數據插入MySQL。 我發現umysqldb插入到MySQL異步中。 但是,我得到以下錯誤: Mysql Error 0: Concurrent access in query method
。
我的代碼如下:
def insert_into_mysql(conn,cur,pid,user,time,content):
try:
value=[pid,user,time,content]
#print 'value is', value
print 'hi'
cur.execute('insert into post(id,user,time,content) values(%s,%s,%s,%s)',value)
print 'after execute'
conn.commit()
# except MySQLdb.Error,e:
except umysqldb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
insert_into_mysql
包含在download_content
:
while len(ids_set) is not 0:
id = ids_set.pop()
print 'now id is', id
pool.spawn(download_content,conn,cur,int(id))
r.sadd('visited_ids',id)
pool.join()
ultramysql不允許您在同一個mysql連接上進行多個查詢,而只是使其異步友好。 因此,您要么為每個greenlet需要一個新的mysql連接,要么使用鎖定原語來確保一次僅一個greenlet正在使用該連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.