Skip to content

Faster Anvil Data Tables via batching, without the boilerplate

License

Notifications You must be signed in to change notification settings

hugetim/auto-batch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

auto-batch

Faster Anvil Data Tables via batching, without the boilerplate.

By default, each Anvil Data Table operation (with the exception of accessing cached information) makes a separate round-trip to the database, which takes on the order of 0.02 seconds. If you are looping through dozens or hundreds of row updates, deletes, or additions, that can add up to noticeable lags. (Moreover, within a transaction, longer execution times are compounded by increasing the likelihood of conflicts triggering one or more re-tries of the entire transaction.) The Accelerated Tables Beta allows you to batch add, update, and delete rows and thus condense many Data Tables operations to a single round trip to the database. But doing so requires changes to your code (even when the Data Table operations are within a transaction, which implies they are meant to be executed together as a block). For example:

import anvil.tables as tables

@tables.in_transaction
def update_cache():
    now = utcnow()
    rows = tables.app_tables.table_1.search()
    with tables.batch_update, tables.batch_delete:                 # batch refactor
        new_log_rows = []                                          # batch refactor
        for row in rows:
            if row['expire_dt'] < now:
                row.delete()
            elif row['next_update_dt'] < now:
                row['next_update_dt'] = now + timedelta(minutes=30)
                new_log_rows.append(dict(id=row['id'], date=now))  # batch refactor
    tables.app_tables.update_log.add_rows(new_log_rows)            # batch refactor

With auto-batch, you get the same performance gains without changing your code, aside from subbing in auto_batch.tables for anvil.tables:

from auto_batch import tables

@tables.in_transaction
def update_cache():
    now = utcnow()
    rows = tables.app_tables.table_1.search()
    print(f"Table 1 has {len(rows)} rows.")
    for row in rows:
        if row['expire_dt'] < now:
            row.delete()
        elif row['next_update_dt'] < now:
            row['next_update_dt'] = now + timedelta(minutes=30)
            tables.app_tables.update_log.add_row(id=row['id'], date=now)

However, auto-batch is not yet production-ready. Use at your own risk. Help in development (and especially writing a comprehensive test suite) welcomed!

Setup

Usage

Use auto_batch.tables any place you would use anvil.tables, as well as auto_batch.users in places of anvil.users so that get_user and force_login work with auto-batch's wrapped Table rows. Table operations within transactions will then be batched automatically. To batch adds, updates, and deletes outside of transactions, you can use the AutoBatch context manager:

with auto_batch.tables.AutoBatch():
    ...

Unfortunately, some Tables functionality is not yet supported. Otherwise, if you only supported features, auto-batch should work as a plug-in substitute for anvil.tables in your app:

  • anvil.tables
    • app_tables
    • Exceptions
    • Functions (esp. in_transaction)
    • Row
    • SearchIterator
      • to_csv
    • Table
      • add_row
      • add_rows
      • client_readable
      • client_writeable
      • client_writeable_cascase
      • delete_all_rows
      • get
      • get_by_id
      • has_row
      • list_columns
      • search
      • to_csv
    • Transaction
  • anvil.tables.query
  • anvil.users

Built With

  • Anvil - web apps built entirely with Python (See below for more.)

License

This project is licensed under the MIT License - see the LICENSE file for details.


Using Anvil to edit this app

The app in this repository is built with Anvil, the framework for building web apps with nothing but Python. You can clone this app into your own Anvil account to test and modify.

First, fork this repository to your own GitHub user account. Click on the top right of this repo to fork it to your own account.

Syncing your fork to the Anvil Editor

Then go to the Anvil Editor (you might need to sign up for a free account) and click on “Clone from GitHub” (underneath the “Blank App” option):

Clone from GitHub

Enter the URL of your forked GitHub repository. If you're not yet logged in, choose "GitHub credentials" as the authentication method and click "Connect to GitHub".

Clone App from Git modal

Finally, click "Clone App".

The app will then be in your Anvil account, ready for you to run it or start editing it! Any changes you make will be automatically pushed back to your fork of this repository, if you have permission! You might want to make a new branch.

Running the app yourself:

Find the Run button at the top-right of the Anvil editor:

More about Anvil

If you’re new to Anvil, welcome! Anvil is a platform for building full-stack web apps with nothing but Python. No need to wrestle with JS, HTML, CSS, Python, SQL and all their frameworks – just build it all in Python.

Learn About Anvil In 80 Seconds👇

Anvil In 80 Seconds

Try Anvil Free

To learn more about Anvil, visit https://anvil.works.

Tutorials

If you are just starting out with Anvil, why not try the 10-minute Feedback Form tutorial? It features step-by-step tutorials that will introduce you to the most important parts of Anvil.

Anvil also has tutorials on:

Reference Documentation

The Anvil reference documentation provides comprehensive information on how to use Anvil to build web applications. You can find the documentation here.

If you want to get to the basics as quickly as possible, each section of this documentation features a Quick-Start Guide.

About

Faster Anvil Data Tables via batching, without the boilerplate

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages