Server took too long

I want to run a function for a atleast a minute, but in anvil server timeout is of 30 seconds. I read the docs about Background tasks. I don’t understand how to proceed.


client (browser) code:

task_object ='generate_take_a_long_time_task')

server code:

def take_a_long_time_task():
  #this takes a long time
  time.sleep(35) # just an example
  results_of_stuff = 42
  return results_of_stuff

def generate_take_a_long_time_task():
  task = anvil.server.launch_background_task('take_a_long_time_task')
  return task

if you then use a timer in your client browser to repeatedly check the results of task_object.get_return_value() , when the result of this is no longer None , it will have the results of your long running function.

If you are using the free plan, background tasks also still have a 30 second limit, unless you use uplink, and run a script from your own computer (or any other internet connected cloud product/machine) that can run your background tasks for an unlimited time.


Hi, Thank you so much!

I’m using a paid plan and what I’m trying to do is calling a server function on click event which launches the background task and I get this error


//client side

//server side

def launch_slow_request_task(detvalue,brcode,listmst):
  task = anvil.server.launch_background_task('insert_into_det_1', detvalue, brcode, listmst)
  return task

def insert_into_det_1(detvalue,brcode,listmst): 
  # print(listmst)
....................... the rest of the function

You don’t really “call” a background task, you “launch” it.

The value returned by calling/launching the task is a BackgroundTask object, not the value returned by the task.

There are several ways for the app to check if the task is still running or has crashed, to check the progress and to retrieve the returned value.

I hope this quick answer clarifies the difference between background and normal tasks, and now you can find the way that best fits you in the documentation or in the forum.

Please don’t ask a question inside another unrelated one. The issue about the returned value has nothing to do with the subject of this post. This should have been its own question with its own answer, and would have helped other forumers in the future. Instead it will remain buried under the wrong subject.

1 Like

+1 for everything @stefano.menci said, the error you are getting is literally because you are trying to unpack the single task object into two objects, since the task object you are returning ‘is not iterable’ you get the error:

You are launching the function launch_slow_request_task which returns a single object called task, but then you are trying to assign it to two variables, with rcot,refno=.
It is only one thing (a background task object), not two.

You will have to periodically check the results of the task object (returned from your'launch_slow_request_task' ... code) to obtain values for rcot and refno.
When the task object no longer returns None as the result of a .get_return_value() call on it, you will have those values instead of None (If the rest of your code works, obviously)

1 Like