Anvil Extras, Nonblocking or asyncio

Apologies in advance, this is a stream of brain farts and ultimately I was looking for peoples use cases for the two and what might be better.

I have an app that has lots of users sharing data that might be updated by another user. If the change by one affects another, that data is shared, crowd sourced data as such.
My intention is to email other users about the change so they can log in and view it and decide to take any actions. I’m using transactions for this to avoid database conflicts if users are updating data another is using etc.

I’m a neat freak… I like to keep my functions in separate modules, so all my email functions are in one module and database modules in another and import the ones I want from the email modules and call them within a database function that then searches a datatable looking for linked users, builds reports, csv’s, fires out emails etc (it could probably be more streamlined…) but this is slowing things down as I need to wait for the email function to finish before carrying on and returning data to the client for the current user who is making changes.

So, I was looking at the Nonblocking module of Anvil Extras (I’m using other modules from it) so from a server function I can call the email function that gathers the users from the database linked that same data, do its job whilst the function that called it carries on doing its thing. From what I can tell Nonblocking and asyncio are similar, but is there any advantage of one over another in this use? I have read numerous posts and as much as I can on each and should really just stop reading and start testing…

Or, should I just use background tasks? Which is where I started and then went down a rabbit hole :joy:

It looks like you might want to use a table to queue up unfinished work, e.g., for a scheduled or background task. Unless you really do need

2 Likes

I vote for background tasks!

3 Likes

My impression of non-blocking was that it was designed to be used from the client to make server calls while the client continued doing work. I don’t believe it works from server function to server function, since that’s what background tasks are for.

I’d suggest you start with background tasks, since that’s a relatively simple mechanism to see if it fits your needs. Be aware that as you scale, you might find that creating a lot of background tasks runs you up against memory limits. If that happens, then you can look at refactoring using a single long-running background task that works its way through a queue of tasks.

1 Like

Yes, I did read that and wonder about Nonblocking only being from client side based on the guides, it doesn’t explicitly say, but that’s where me actually testing something would be a good idea… :wink:
Background tasks it is! Thanks to you all!