Passing data from server to client

Hello,

I am trying to pass information from server side to client to inform the progress of the rows processed (in a label) as the code below.
In server when print row it shows number of rows incremental.
How can I pass this information in client side. I can’t use timer because the server is called once for all the process.
I tried to use return row but after first row the server obviously stops.
The data from file can’t be passed on table to do a count in client side because the process I am using is selenium in uplink server.

Code Sample:

Client side
anvil.server.call(‘function’,file)
Server
Def function (file):
      with open file …
      read_excel(pandas…)
      for row in range(max_row):
           do something

           row = row + 1
           print(row)

Thank you

If I understand you want the client to start a function that will run for a long time and show the progress while it runs.

The snippet you show has a second problem, other than not being able to report the progress: it will crash if function takes longer than 30 seconds.

I do this with a timer on the client and 3 server functions:

  • the client calls the server function start_process and starts a timer
  • the server function start_process launches the background task background_process and returns immediately
  • background_process creates a row in a table where it writes the progress, just like your print, but to a table row; when the process is complete, it stores "done" in the progress row, and maybe some return value in another column
  • the tick event calls the server function check_progress every 2 seconds and shows the returned value on a label or progress bar
  • the check_progress function returns the row updated by background_process and, if the status is "done", stops the timer

It is possible to store the status of the background task in the background task itself as described in the documentation, but I use a table because in the past it was impossible to get the id of the background task from the background task itself and because there were performance problems when running hundreds of background tasks per day (I don’t remember the details, it had something to do with the list of completed tasks containing the returned value from all the tasks being too slow to process).

I think that today both the problems have been addressed and you may be able to use the background tasks as described on the documentation. I don’t know yet, I will know it the next time I need it :slight_smile:.

1 Like

Thanks a lot for your help

I forgot to mention that background tasks have the 30 second max duration limit if you are on the free plan, otherwise they have no time limit (I think)

I have gone as long as 7, 12, even 14, 17 or even 23 hours with no problem.

I also wonder if the 30 second timeout on background tasks applies if the background task @decorated function is running on an uplink. (if you are on the free plan)

I am on paid plan but use uplink because of selenium package

I don’t see it stated explicitly in the documentation, but it appears (from the code snippet comment in Defining a Background Task) that background tasks can run only on the Server, not in an Uplink program. It might be triggered from Server or Uplink, but it executes on the Server only.

I used to run background tasks on uplink scripts, but the connection problems would make managing failures on background tasks too difficult.

Now I only start the background task in the server, then the task will call uplink functions.

1 Like