How to get media generated from background task to the client side

Great, as long as you don’t go over the row limit for the free plan with users or any other limits if you are on the paid plans then it should work forever. (Most use cases will not see 50000 or 150000+ different users but who knows)

If someone else is reading this later and is interested in how to clean up the table (delete old rows) here is some code:

from datetime import datetime, timedelta
import anvil.tz

when creating a new request for your media object and inserting it into a new row for a user have a ‘date and time’ column and add something like this to the row insert:

last_update_time=datetime.now(anvil.tz.tzutc())

For example mine looks like this:

app_tables.requests_handler.add_row(
                                    pending=True,
                                    request_id=unix_time_microseconds_int,
                                    user_email=anvil.users.get_user()['email'],
                                    media_file=my_blobmedia_object,
                                    last_update_time=datetime.now(anvil.tz.tzutc())
                                      )

then make a callable function in the server module like:

@anvil.server.callable
def cleanup_unused_files():
  number_of_hours_before_cleanup = 24

  rows = app_tables.requests_handler.search(last_update_time=q.less_than_or_equal_to(datetime.now(anvil.tz.tzutc() ) - timedelta(hours=number_of_hours_before_cleanup) )  )

  if len(rows) > 0:
    for row in rows:
      row.delete()
      
  return 

Then include a call to this function whenever someone requests your media file.

You could also do without the @anvil.server.callable if you included this in whatever server module function created or stored the media object, since they would be running in the same place.

1 Like