Skip to content

Commit bf10027

Browse files
authored
Add initial docs to README.md
1 parent c1a16e3 commit bf10027

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,87 @@
11
# auto-batch
22

3-
Faster Anvil Data Tables via batching, without the boilerplate. See [forum post](https://anvil.works/forum/t/auto-batch-table-operations/21376/) for more.
3+
Faster Anvil Data Tables via batching, without the boilerplate.
4+
5+
By default, each [Anvil Data Table](https://anvil.works/docs/data-tables#using-data-tables-from-python) 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](https://anvil.works/docs/data-tables/transactions#using-a-decorator), 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](https://anvil.works/docs/data-tables/accelerated-tables#new-features) 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:
6+
7+
```python
8+
import anvil.tables as tables
9+
10+
@tables.in_transaction
11+
def update_cache():
12+
now = utcnow()
13+
rows = tables.app_tables.table_1.search()
14+
with tables.batch_update, tables.batch_delete: # batch refactor
15+
new_log_rows = [] # batch refactor
16+
for row in rows:
17+
if row['expire_dt'] < now:
18+
row.delete()
19+
elif row['next_update_dt'] < now:
20+
row['next_update_dt'] = now + timedelta(minutes=30)
21+
new_log_rows.append(dict(id=row['id'], date=now)) # batch refactor
22+
tables.app_tables.update_log.add_rows(new_log_rows) # batch refactor
23+
```
24+
25+
With auto-batch, you get the same performance gains without changing your code, aside from subbing in `auto_batch.tables` for `anvil.tables`:
26+
27+
```python
28+
from auto_batch import tables
29+
30+
@tables.in_transaction
31+
def update_cache():
32+
now = utcnow()
33+
rows = tables.app_tables.table_1.search()
34+
print(f"Table 1 has {len(rows)} rows.")
35+
for row in rows:
36+
if row['expire_dt'] < now:
37+
row.delete()
38+
elif row['next_update_dt'] < now:
39+
row['next_update_dt'] = now + timedelta(minutes=30)
40+
tables.app_tables.update_log.add_row(id=row['id'], date=now)
41+
```
42+
43+
However, auto-batch is not yet production-ready. Use at your own risk. Help in development (and especially writing a comprehensive test suite) welcomed!
44+
45+
## Setup
46+
47+
* Ensure that you have [enabled Accelerated Tables](https://anvil.works/docs/data-tables/accelerated-tables#enabling-the-accelerated-tables-beta) for your app.
48+
* Add auto-batch to your app as a [third-party dependency](https://anvil.works/forum/t/third-party-dependencies/8712) with the token `PKF2MZRQMPCXFWNE`.
49+
50+
## Usage
51+
52+
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:
53+
54+
```python
55+
with auto_batch.tables.AutoBatch():
56+
...
57+
```
58+
59+
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:
60+
61+
- [anvil.tables](https://anvil.works/docs/api/anvil.tables)
62+
- [x] `app_tables`
63+
- [x] Exceptions
64+
- [x] Functions (esp. `in_transaction`)
65+
- [x] `Row`
66+
- `SearchIterator`
67+
- [ ] `to_csv`
68+
- `Table`
69+
- [x] `add_row`
70+
- [x] `add_rows`
71+
- [ ] `client_readable`
72+
- [ ] `client_writeable`
73+
- [ ] `client_writeable_cascase`
74+
- [x] `delete_all_rows`
75+
- [x] `get`
76+
- [x] `get_by_id`
77+
- [ ] `has_row`
78+
- [x] `list_columns`
79+
- [x] `search`
80+
- [ ] `to_csv`
81+
- [x] `Transaction`
82+
- [x] [anvil.tables.query](https://anvil.works/docs/api/anvil.tables.query)
83+
- [x] [anvil.users](https://anvil.works/docs/api/anvil.users)
84+
485

586
### Built With
687

0 commit comments

Comments
 (0)