I need to populate a SQLite database every few minutes in Django, but I want to serve stale data until the data is available for the database to be updated. (i.e. I don't want to block for the data to be gathered; the only time I can block is if there is a lock on the database, during which I have no choice.)
I also don't want to install a separate program or library.
How would I go about setting up another thread that could call save()
on a bunch of models, without running into threading issues?
If you're looking for a lightweight solution for just executing stuff in background rather than a full-blown task management system, take a look at django-utils. It includes, among other things, an @async function decorator that will make a function execute asynchronously in a separate thread.
Use it like this:
from djutils.decorators import async
@async
def load_data_async():
# this will be executed in a separate thread
load_data()
Then you can call either the load_data_async function
for background, or the normal load_data
function for blocking execution.
Just make sure to install a version before 2.0, since that lacks the @async decorator.
Note: If even installing django-utils would be too much, you can simply download it and include the few required files in your project.