Server Timeout using Background Task

What I’m trying to do:
Upload a 600 kb excel file to the data table and the server is timing out. I understand the 30 second timeout is by design, but I thought that uploading it via Background Task would resolve that issue. My Background Task is now timing out.

What I’ve tried and what’s not working:

Code Sample:

SERVER SIDE CODE:

@anvil.server.callable
def store_data(file):
  with anvil.media.TempFile(file) as file_name:
    if file.content_type == 'text/csv':
      df = pd.read_csv(file_name)
    else:
      df = pd.read_excel(file_name)
    for d in df.to_dict(orient="records"):
      app_tables.uploads.add_row(**d)
      
      

@anvil.server.background_task
def make_slow_request(file):
  upload = anvil.server.call('store_data', file)



@anvil.server.callable
def launch_slow_request_task(file):
  task = anvil.server.launch_background_task('make_slow_request', file)
  return task



CLIENT SIDE CODE:

  def file_loader_1_change(self, file, **event_args):
    anvil.server.call('launch_slow_request_task', file)
    pass


The error is shown below. Am I misunderstanding that a Background Task is a way to get around the 30 second limit?

Clone link:
share a copy of your app

While you are correct that a background task will get around the 30 second limit, you need to have the long running code under the background task decorator. You can try this:

SERVER SIDE CODE:
@anvil.server.background_task
def make_slow_request(file):
  with anvil.media.TempFile(file) as file_name:
    if file.content_type == 'text/csv':
      df = pd.read_csv(file_name)
    else:
      df = pd.read_excel(file_name)
    for d in df.to_dict(orient="records"):
      app_tables.uploads.add_row(**d)


@anvil.server.callable
def launch_slow_request_task(file):
  task = anvil.server.launch_background_task('make_slow_request', file)
  return task



CLIENT SIDE CODE:

  def file_loader_1_change(self, file, **event_args):
    anvil.server.call('launch_slow_request_task', file)
3 Likes

Ah, makes complete sense. It’s working now. Thanks @robert.

1 Like

Glad you got it working!

1 Like