Django后台任务

编程入门 行业动态 更新时间:2024-10-22 12:31:08
本文介绍了Django后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在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后台任务

本文发布于:2023-11-15 05:45:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:后台   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!