django - 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 个并行数据开始处理,并且都触发了电子邮件,这不是所需的行为。
有没有办法避免这种情况?那么对于用户会话,应用程序应该只在所有旧数据处理完成后才接受数据?
解决方案
由于我们在使用伪代码进行讨论,因此一种方法是使用单例模式并triggerapi()
返回Not Allowed以防它已经被实例化。
推荐阅读
- maven - 所有软件包的 Maven 匹配器是什么?
- python - 如何加快敏感性分析?
- javascript - 为什么 dateFormat 会导致“Uncaught ReferenceError: dateFormat is not defined”错误?
- sql - SQL Server 拉丁重音敏感排序规则
- c++ - 如何将定义添加到 moc_predefs.h?
- ruby-on-rails - 为什么我在运行 rails 控制台时会收到此错误?
- asp.net-core - 无法通过 http 获取 steeltoe 健康执行器端点
- android - 模型如何将数据更改传达给android mvvm中的视图模型?
- phpmailer - 使用 PHPMailer 发送邮件的问题
- ios - 来自外部源的 iframe 内容未加载到 iOS 上的本地 HTML 文件中