我使用twisted.enterprise
的adbapi
提供的数据库连接池来实现并行数据库写入,如下所示
dbpool = adbapi.ConnectionPool('pymysql', **config)
query = dbpool.runInteraction(insert, item)
query.addErrback(handler_error)
def insert(cursor, item):
cursor.execute('SELECT id from area where name = %s', (item['area'],))
area_id = cursor.fetchone()[0]
cursor.execute('SELECT id from location where name = %s', (item['location'],))
location_id = cursor.fetchone()[0]
insert_sql = 'INSERT INTO `community` (`community_id`,`name`, `area_id`, `location_id`) VALUES (%s, %s, %s, %s)'
params = (item['community_id'], item['name'], area_id, location_id)
cursor.execute(insert_sql, params)
def handler_error(self, failure):
print('--------------database operation exception!!-----------------')
print(failure)
我知道adbapi connectionPool为每个方法/线程提供一个连接,这意味着每个insert
都有一个db连接。但是,insert
方法中有三个sql,它们共享相同的连接和游标。你知道吗
我搜索了google,发现InterfaceError (0, '')
问题可能是因为在sql执行之前关闭了游标。你知道吗
在我的情况下,这是因为我在同一个方法中有三个sql导致了InterfaceError (0, '')
异常。有什么办法处理吗?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐