Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python将异步插入MySQL 解决了这个问题,每个greenlet应该有一个连接,而不是共享同一个连接。_Python_Mysql_Gevent - Fatal编程技术网

如何使用Python将异步插入MySQL 解决了这个问题,每个greenlet应该有一个连接,而不是共享同一个连接。

如何使用Python将异步插入MySQL 解决了这个问题,每个greenlet应该有一个连接,而不是共享同一个连接。,python,mysql,gevent,Python,Mysql,Gevent,我想在MySQL数据库中插入大量数据。我使用gevent从internet下载数据,然后将数据插入MySQL。我发现要插入MySQL异步。但是我得到了以下错误:Mysql错误0:query方法中的并发访问 我的代码如下: def insert_into_mysql(conn,cur,pid,user,time,content): try: value=[pid,user,time,content] #print 'value is', value

我想在MySQL数据库中插入大量数据。我使用
gevent
从internet下载数据,然后将数据插入MySQL。我发现要插入MySQL异步。但是我得到了以下错误:
Mysql错误0:query方法中的并发访问

我的代码如下:

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\u-into\u-mysql
包含在
download\u内容中

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在使用该连接

您可以对标准的mysqldb驱动程序进行monkey补丁,它应该是异步的,还可以看到umysqldb是一个围绕ultramysql的mysqldb兼容包装器。