Trigger Timer on function call

You cannot make a call for a task that lasts longer than 30 seconds.
You do need to use a background task.
Your background task will call the uplink function, the uplink function will run for 40 minutes and return the control to the background task.

While the uplink function runs, it should update a row in the database every few seconds, and your timer should check that row.

Make sure interval is set to 0 on the form, so the timer doesn’t tick when the form opens.

You could do something like this:

# on the form
def file_loader_1_click(self, file, **event_args):
    self.timer_1.interval = 2
    anvil.server.call("start_the_job", job_id)

def timer_1_tick(self, **event_args):
    status = anvil.server.call_s("get_status", job_id)
    self.label_1.text = status
    if status == 'done':
        self.timer_1.interval = 0


# on the server
@anvil.server.callable
def start_the_job(job_id):
    row = app_tables.table_name.add_row(job_id=job_id, status='starting')
    anvil.server.launch_background_task('uplink_task', job_id)

@anvil.server.callable
def get_status(job_id):
    row = app_tables.table_name.get(job_id=job_id)
    return row['status']


#uplink
@anvil.server.background_task
def uplink_task(job_id):
    row = app_tables.table_name.get(job_id=job_id)
    for i in range(1000):
        row['status'] = f'step {i}'  # make sure this doesn't execute more than once every 2-3 seconds
        # do something slow
    row['status'] = 'done'

I have done something similar years ago, and if I remember correctly running background tasks on uplink wasn’t a smart idea.

This is pseudocode I wrote very quickly. I’m sure it will not work as is, it’s there just to give you an idea.

1 Like