通知Django运行任务 [英] Signal django to run a task

查看:130
本文介绍了通知Django运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个管理命令,从csv文件填充我的一个模型。

我需要经常进行此更新,并且csv文件有成千上万行。

有时可能需要10分钟以上才能完成填充。

I made a management command that populates one of my models from a csv file.
I need to do this update quite frequently and the csv files have tens of thousands of lines.
Sometimes it can take over 10 minutes to finish populating.

我想添加一个功能,使我可以直接通过网站上传csv文件,并且在文件上传后,django应该运行该命令,或者至少要执行逻辑从它,并填充数据库。

I want to add a feature that lets me upload the csv file through the website directly and after the file is uploaded, django should run that command, or at least the logic from it, and populate the db.

我将如何处理?我希望能够在上传文件后离开页面,并在任务完成后收到电子邮件。

How would I go about this? I want to be able to leave the page after I upload the file and receive an e-mail when the task is finished.

推荐答案

您可以使用 Django背景任务。它是Django的数据库支持的工作队列。

You can do the same with Django Background Task. Its a databased-backed work queue for Django. And is easy to implement than Celery.

from background_task import background
@background(schedule=60)
def your_task():
    # do your cool work here.

这会将您的任务转换为背景任务功能。当您从常规代码调用它时,它将实际上创建一个Task对象并将其存储在数据库中。

This will convert the your_task into a background task function. When you call it from regular code it will actually create a Task object and stores it in the database.

这篇关于通知Django运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆