Improving insert_or_update_page()

OperationalError: (OperationalError) (1205, 'Lock wait timeout exceeded;
try restarting transaction')