簡體   English   中英

如何使用Python將異步插入MySQL

[英]How to insert async into MySQL using Python

解決后,每個greenlet應該具有一個連接,而不是共享同一連接。

我想在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM