Accessing multiple background tasks from the client side

Hello

I am using background tasks in my app and I am using more than one task in more than one form; my problem was I was not able to track these different tasks where I want to access one task in one form and others in one another form. I would like to know how I could implement this in anvils client side.

Thanks in advance

In general, you can get a list of all background tasks: Anvil Docs | Background Tasks

If you want it finer grained than that (e.g. form A knows about tasks A and B, while form B knows about task C), you need to capture the task returned when you call anvil.server.launch_background_task and get that task reference to the form that needs it.

@jshaffstall thanks for your kind response, I have tried to save the second task id on global variable and access it on the other form but unfortunately not able to get task by id on the client side.
Also I have been trying to pass the instance of the task to the other form but not able to access the task data yet

  def timer_tick(self, **event_args):
    self.timer_1.interval = 0 # stop the timer while we check the background task
    my_data = None
    if self.task:
      with anvil.server.no_loading_indicator:
        my_data = self.task.get_return_value() # None if nothing is yet returned
    if my_data is not None:
        GlobalVars.x_data= my_data
        self.task = None 
    else:
      self.timer_1.iterval = 1

  def timer_projects(self, **event_args):
    self.timer_2.interval = 0 
    alert("here")
    my_data = None
    if task_2:
      with anvil.server.no_loading_indicator:
        my_data = task_2.get_return_value() # None if nothing is yet returned
    if my_data is not None:
        GlobalVars.y_data= my_data
        task_2 = None         
    else:
      self.timer_2.iterval = 1

At some point you need to call a server function to start the background task. That server function must return the background task to the client, so it can be saved in self.task.

I have already done so on my init funcion
and the first background task is working fine

Here is init funtion

  def __init__(self, task_1, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    global task_2
    task_2 = task_1
    self.task = anvil.server.call_s('get_x_data_back',"EUR")
    self.timer_1.interval = 1

and here is the content of my background task in the server side

@anvil.server.callable
def get_x_data_back(currency, time):
  task = anvil.server.launch_background_task('get_x_data_backgrond',currency, time)
  return task

@anvil.server.background_task
def get_x_data_backgrond(currency, time):
  # here I am calling the function on my external server 
  my_data = anvil.server.call('get_x', currency,time)
  return my_data



@anvil.server.callable
def get_x_data_back():
  task = anvil.server.launch_background_task('get_y_data_backgrond')
  return task

@anvil.server.background_task
def get_y_data_backgrond():
  #Also here I am calling the function on my external server 
  my_data = anvil.server.call('get_y')
  return my_data

Is this an app you can share a clone link for? From your description I have no idea which form is starting which background task, and how you’re trying to transfer information between them (none of the code you posted shows opening another form). Seeing the entire app would help a lot.

Thanks @jshaffstall for your kind follow up, but I am sorry I can’t share a copy of the app where I can share the entire process

This is the very first HomePage

class Homepage(HomepageTemplate):
 def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    self.task = anvil.server.call_s('get_x_back')
    self.load_home_page_cont()
      
 # here is a function that loads the first HomePage content
  def load_home_page_cont(self):
    self.content_panel.add_component(HomePageContent(task_2 = self.task), full_width_row=True)

And this is the HompePageContent page which loads the last page (our destination)

class HomePageContent(HomePageContentTemplate):
  def __init__(self, task_2, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    self.column_panel_1.add_component(LastPage(task_2), full_width_row=True)
    

And finally this is the last page where I am trying to access both of the tasks

class LastPage(EURRatesTemplate):
  def __init__(self, task_1, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)

    global task_2
    task_2 = task_1
    self.task = anvil.server.call_s('get_y_back',"EUR")
    self.timer_1.interval = 1

def timer_tick(self, **event_args):
    self.timer_1.interval = 0 # stop the timer while we check the background task
    my_data = None
    if self.task:
      with anvil.server.no_loading_indicator:
        my_data = self.task.get_return_value() # None if nothing is yet returned
    if my_data is not None:
        GlobalVars.x_data= my_data
        self.task = None 
    else:
      self.timer_1.iterval = 1

  def timer_projects(self, **event_args):
    self.timer_2.interval = 0 
    alert("here")
    my_data = None
    if task_2:
      with anvil.server.no_loading_indicator:
        my_data = task_2.get_return_value() # None if nothing is yet returned
    if my_data is not None:
        GlobalVars.y_data= my_data
        task_2 = None         
    else:
      self.timer_2.iterval = 1

So far here is the entire background_task server side

@anvil.server.callable
def get_x_data_back(currency, time):
  task = anvil.server.launch_background_task('get_x_data_backgrond',currency, time)
  return task

@anvil.server.background_task
def get_x_data_backgrond(currency, time):
  # here I am calling the function on my external server 
  my_data = anvil.server.call('get_x', currency,time)
  return my_data

@anvil.server.callable
def get_x_data_back():
  task = anvil.server.launch_background_task('get_y_data_backgrond')
  return task

@anvil.server.background_task
def get_y_data_backgrond():
  #Also here I am calling the function on my external server 
  my_data = anvil.server.call('get_y')
  return my_data

Some initial comments, I don’t know if you copied code or retyped it, but you’re calling anvil.server.call_s('get_x_back') and haven’t defined a server function with that name. Same with anvil.server.call_s('get_y_back',"EUR").

You’re also redefining get_x_data_back in your server code, the second one is probably supposed to be get_y_data_back.

The global variable usage is also suspect, since you’re not declaring task_2 to be global in all the functions you’re modifying it. You should probably just use a self.task_2 variable instead.

I don’t see how any of this is running at the moment, unless there are server functions you’re not showing, e.g. get_x_back and get_y_back, or there are typos in the code you posted.

Maybe let’s get back to the original issue…when you run the app, do you get error messages? If so, what are those error messages. If not, what tells you that what you have isn’t working?

Thanks a lot @jshaffstall for your great comments and suggestions, yes I have changed the names a little bit in order to make it simple, so far the issue was not in the handling of the task in task_2 it was also on the background_task server side where the server function is experiencing an error when calling a specific function this function gets data from data tables and return it back to task_2.get_return_value() this also was not working but your suggestion to self.task_2.get_return_value() worked fine, so the error was when calling this return function for the function above

Here is the function returning the error

@anvil.server.callable
def get_projects():
  me = anvil.users.get_user()
  return app_tables.projects.search(tables.order_by("created", ascending=False), user=me, approved= False)
  

And when calling self.task_2.get_return_value() this error prompts

AppOfflineError: Connection to server failed (1006)
at HomePageContent.EURRates, line 57
called from HomePageContent.EURRates, line 57
AppOfflineError: App is offline
at Homepage, line 183

I do really appreciate your kind help and to be honest you already solved the main issue I have posted for but would be more happier if you could help me fixing this error.

Thanks a lot

Remember, you don’t have to wait for someone to respond. You can search this forum for:

and see where those answers lead.

@p.colbert You are right I had already checked it out but missed to come back and close this issue
Thanks

1 Like