多亏了@romanperekhrest,这里有一个
solution
来自mysql手册
LOCK/UNLOCK TABLES
.
engine = create_engine('mysql://user:pass@%s/db?charset=utf8' % host)
conn = engine.connect()
trans = conn.begin()
try:
conn.execute('set autocommit=0')
conn.execute('lock tables tests write, data write')
conn.execute('delete from tests where ml="%s"' % ml)
tests.to_sql(con=conn, name='tests', if_exists='append', index=False)
data.to_sql(con=conn, name='data', if_exists='append', index=False)
trans.commit()
conn.execute('unlock tables')
print(CGRE + ml + ': OK' + CEND)
except:
trans.rollback()
conn.execute('unlock tables')
conn.close()
print(CRED + ml + ': database error!' + CEND)
raise
conn.close()