【问题标题】:How to use thread in Django如何在 Django 中使用线程
【发布时间】:2016-04-28 15:50:03
【问题描述】:

我听过线程,但我不知道如何使用它以及将函数放在哪里。我有一个发送电子邮件的应用程序邮件,我想在运行服务器时控制温度,如果温度高于最高温度,则发送电子邮件。 邮件/views.py

from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect


def index(request):
    subject = request.POST.get('subject', 'subject')
    message = request.POST.get('message', 'attention ! la temperature a depasse le maximum ')
    from_email = request.POST.get('from_email', '***********@gmail.com')
    if subject and message and from_email:
        try:
            send_mail(subject, message, from_email, ['*********@gmail.com'])
            return HttpResponse('templates/mail.html')
        except BadHeaderError:
            return HttpResponse('Invalid header found.')
        return HttpResponseRedirect('mail')
    else:

        return HttpResponse('Make sure all fields are entered and valid.')

【问题讨论】:

  • 如果我们想在后台运行一些东西,看看使用 Celery
  • 我明白了,但是我的老师强迫我使用线程,但我仍然不知道如何使用它没有找到任何教程谈论

标签: django multithreading


【解决方案1】:

您需要为此使用后台工作,线程对您的情况没有帮助。
以下是您可以使用的库列表:
https://stackoverflow.com/a/36386133/3896300

【讨论】:

  • 我明白了,但是我的老师强迫我使用线程,但我仍然不知道如何使用它没有找到任何教程谈论
  • hhh 我不这么认为:)
  • 您究竟需要如何使用它们?这个问题有点模糊
  • 我有一个温度传感器。我希望当我运行服务器时,如果温度超过 25,我正在运行的应用程序是什么,例如它会发送一封电子邮件(它直接访问我的邮件应用程序)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-05
  • 2023-02-16
  • 2023-04-09
  • 2018-11-17
  • 1970-01-01
相关资源
最近更新 更多