anvil.server.SerializationError: Illegal value in a anvil.server.task_state. Cannot use BlobMedia objects in task state

What I’m trying to do:
From a client call I generate up to 9 matlib plots on the server side. This takes between 2 and 8 seconds per plot, depending, I guess, on the load of the server (I am on the hobby plan). In any case, I have no control over how long it takes. When the server needs more time (more than 30 sec?) I get a server.Timeout error.
I need these plots (as pngs) to fill slots in a repeating panel on the client side.

What I’ve tried and what’s not working:
So I wrapped the function that builds the matlibplots in a background task. (Guided by this tutorial Running Tasks in the Background) When I try to access the results of the task, I get an error saying tasks cannot return SimpleObjects.
Code Sample:

    plot_list = []
    for var_row in vars_info_rows:
      fdz = build_plot(var_row, regidx, cap)
      plot_list.append(fdz)
      print ('Printin type(fdz) fdz type(plot_list)')
      print (type(fdz))
      print(fdz)
      print(type(plot_list))
      anvil.server.task_state['plots'] = plot_list

type (fdz):
<class ‘dict’>
fdz:
{‘title’: ‘ETI-12: Responsible consumption and production’, ‘subtitle’: ‘Nitrogen use (kg/ha-year)’, ‘fig’: BlobMedia[image/png,14794 bytes,name=plot.png], ‘cap’: ‘mov240906 mppy GAME e4a 10reg.mdl on Sat 01 Mar 2025’}
type(plot_list)
<class ‘list’>

The problem is the fig entry in the dictionary, the [image/png], since all works as advertised if I do not put the image/png into the dictionary to be returned.

How do I return a png through a task.state back to the client?

Thanks for taking the time to think about this.
Clone link:
no idea how to do this, sorry

EDIT:
The line anvil.server.task_state[‘plots’] = plot_list
is outside the for loop

Hit the submit button too soon. This solved it:

1 Like