Connection to server failed (1006)

What I’m trying to do:
Hello! I started to use anvil few days ago and it is really good. I want to call a function in server using “anvil.server.call” but this problem keeps happening. I searched and found someone encountered this problem due to the function in server takes too long to execute. (AppOfflineError if the browser is busy with long running functions)
I think this is the reason but I cannot shorten the time to execute this function so I want to know if there are any methods that can help me solve this?

What I’ve tried and what’s not working:
I used the solution in the post which is insert “time.sleep(0)” every so often in the client side function.
It helps when the time needed to execute not too long(10 to 15 minutes). But when the server takes 20 to 50 minutes to execute the function, the error occurs.

Code Sample:

def button_click(self, **event_args):
    """This method is called when the button is clicked"""
    time.sleep(0)

    # example (some checking in the code)
    if self.label_1.text == "abc":
       self.label_1.text = ...
        
    time.sleep(0)  

   # some checkings just like the if statement above
    
    time.sleep(0)
    
    # this function takes 10 to 50 minutes to finish
    anvil.server.call("my_function", ...)

Thanks!! (Sorry for my bad english.)

When a server call takes longer than 30 seconds you should make it a background task.

The form calls a server function, the server function starts the background task and returns immediately.

Then the form has a timer with the tick event that calls another server function that checks the status of the background task every few seconds and updates the interface accordingly.

EDIT
Background tasks are limited to 30 seconds in free plans.

2 Likes

Thanks for the reply!!
But now this occurs:
TypeError: Indexing with [] is not supported on this anvil.private.BackgroundTask

I have an argument (its type is list) that needs to be passed to the background task. Is this the reason why this error happens?

I can’t answer without seeing the code.

Sure. I am sorry about that.

# server

@anvil.server.background_task
def my_background_task(my_list, my_bool, my_image, my_dictionary):
    # my_image is a media from client side 
    return my_function(my_list, my_bool, my_image, my_dictionary) # this returns a list of strings

@anvil.server.callable
def call_task(my_list, my_bool, my_image, my_dictionary):
    return anvil.server.launch_background_task("my_background_task", my_list, my_bool, my_image, my_dictionary)
# client

results = anvil.server.call("call_task", my_list, my_bool, my_image, my_dictionary)
text1 = results[0]
text2 = results[1]

The error is occurring because the anvil.server.launch_background_task returns a background task object (as per the docs).

So the error is occurring because the call_task function is only receiving the background task object not the return of the background task object.

You will need to change the code to poll the background task object (probably using a timer) to fetch the data from my_background_task

Have a read through the doc, they are very good at stepping it out.

You can also find example builds and tutorials in the learn section - this one is particularly helpful for background tasks

3 Likes

@stefano.menci @rickhurlbatt Thank you both!!
I will read the doc and tutorial about it.