首页 > 解决方案 > Django函数执行

问题描述

在视图中,我定义了一个函数,该函数在用户在线提交表单时执行。表单提交后,我执行了一些数据库事务,然后根据数据库 API 中的现有数据触发:

triggerapi():
   execute API to send Email to the user and the administrator about 
   the submitted form
def databasetransactions():
    check the data in the submitted form with the data in DB
    if the last data submitted by the user is before 10 mins or more: 
       triggerapi()
def formsubmitted(request):
    save the user input in variables
    Databasetransactions()
    save the data from the submitted form in the DB

在上述情况下,用户在不到 5 毫秒的时间内点击了 2 次提交按钮。因此 2 个并行数据开始处理,并且都触发了电子邮件,这不是所需的行为。

有没有办法避免这种情况?那么对于用户会话,应用程序应该只在所有旧数据处理完成后才接受数据?

标签: djangopython-3.xfunction

解决方案


由于我们在使用伪代码进行讨论,因此一种方法是使用单例模式triggerapi()返回Not Allowed以防它已经被实例化。


推荐阅读