我需要在Django中每隔几分钟填充一个SQLite数据库,但是我想要提供陈旧的数据,直到数据可用于更新数据库。 (即我不想阻止要收集的数据;只有时间,我可以阻止是如果有一个锁在数据库,在此期间我没有选择。)
我也不想安装单独的程序或库。
我如何设置另一个线程可以在一堆模型上调用 save(),而不会遇到线程问题?
解决方案如果你正在寻找一个轻量级的解决方案,只是在后台执行的东西,而不是一个成熟的任务管理系统,看看 django-utils 。其中包括 @
使用这样:
从djutils.decorators导入async @async def load_data_async ():#这将在单独的线程中执行 load_data()然后你可以调用 load_data_async函数作为背景,或者调用正常的 load_data 函数来阻止执行。 p>
注意:如果甚至安装django-utils会太多,你可以简单地下载它,并在你的项目中包含几个必需的文件。
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.
Note: If even installing django-utils would be too much, you can simply download it and include the few required files in your project.
更多推荐
Django后台任务
发布评论