Eek. No need for any of these shenanigans! Just use a transaction:
# Assume there's a table called 'Counter' with a single column ('value')
# and a single row:
@tables.in_transaction
def get_next_value_in_sequence():
row = app_tables.counter.get()
row['value'] += 1
return row['value']
Anvil’s transactions are fully serialisable, and @tables.in_transaction
automatically retries on conflict, so this function will produce a steady continuous sequence.