Can the same server function be both `callable` and `background_task`?

I have a server function that works in 98% of scenarios, but in some scenarios (where the input data is too large) it causes a server timeout.

What I’m thinking of is catching the anvil.server.TimeoutError and launching a background task instead in the except clause.

To avoid duplicate code, can I decorate the same function with both the @anvil.server.callable as well as the @anvil.server.background_task decorators and use it as either?

Ah nevermind, I realized it would be better to just use a wrapper function for the background task.

2 Likes

Yes, you can use two decorators on the same function.

You just need to be careful, because a server callable used by a form and one run as a background task execute in different contexts. For example, a user may or may not be logged in, so you need to make sure the function works correctly in both situations.

1 Like