Hey guys, I’m a high school student, who’s looking to build a AI Chatbot, trained specifically on Google Drive files (documents, sheets, videos, audio files) using Anvil.
I have two forms in my app right now; one wherein the user can enter their Google Drive folder link, and the other where they can interact with their data.
I am using Background Tasks to load the Google Drive folders into the vector database linked to the chatbot; but for folders with even 3 mp4 files of 30 minutes each, that Background task runs approximately for 15 minutes, to load the data. So I had a couple questions about Background tasks for when I deploy my app to the public:
For multi-tenancy apps, if a user loads a G-Drive folder and the Background Task is launched, will the Background Task be killed if the user closes the app while it is running?
How are Background Tasks executed in Anvil? If multiple users launch multiple background tasks simultaneously, will the Background Tasks be executed sequentially, or parallelly?
Can I launch Background Tasks using Background tasks? Right now, what happens in my G-Drive loader function is that it goes through each file or sub-file sequentially, individually; can I make it so that the code gets all the files/folders from that folder, and then my background task launches more background tasks to get the data from each file and store it in the vector database, as a separate Background task?
I have searched the docs for Background tasks and these questions, but I am unable to find the answer to these questions.
Ambitious project for a high school student! That’s great to see.
Background tasks don’t get killed when an app is closed. If you run your app in a ‘debug’ environment and close that, you get prompted to kill your running background tasks. You can click ‘yes’ or ‘no’. No such prompt exists for other environments.
Background tasks are executed in separate processes, so in parallel.
You should be able to, but I’d be careful not to launch too many background tasks. That kind of loop could easily launch dozens of concurrent background tasks which I’m not sure your app can handle. Personally, in cases like these, I call the other background tasks as functions and keep it all ‘sequential’, as I’m not worried about speed.
I would like to add that, depending on the plan, every account may have a limit on the amount of memory or cpu used.
If you have many background tasks running in parallel and you are using too much cpu, I think (not sure) your app will go slower.
If you are using too much memory, I think something will be killed. I don’t know if one of the tasks or the whole app.
There is no such thing like the user closing the app. The user may close the browser or turn off the computer, but the background tasks running in the server don’t know about it.
Things are different when you run inside the ide. In that case there is a stop button and the ide knows that the app is being stopped.
The reason I asked about if background tasks are terminated after the window is closed, is because when I’m trying to process videos (or mp4 files in general) and store them as TempFile that can be processed (split into multiple audio files) using PyDub, server code always “quit[s] unexpectedly”, so I was concerned about that.
After reading this, I checked using tracemalloc. The peak usage where the server quits is around 4 GB; is that too much memory for the Personal Plan?
I can reduce the memory usage by splitting the Media object in two, and then passing them sequentially using asyncio, if that would be possible [Don’t know if Asynchronous programming is available on Anvil]