gpt4 book ai didi

multithreading - 使用Python 2.7的GAE上的线程引发 “FrontendsNotSupported”错误

转载 作者:行者123 更新时间:2023-12-03 12:52:38 25 4
gpt4 key购买 nike

我将GAE上的应用程序从Python 2.5迁移到了Python 2.7。

Python 2.7允许线程化,我正在尝试创建一个线程在后台发送电子邮件。这里的线程:

from google.appengine.api import background_thread
from django.core.mail import send_mail

subject = 'Hello!'
msg = '\n \n Hello World!'
sender = settings.DEFAULT_FROM_EMAIL
to = 'xx@xx.com'

t = background_thread.BackgroundThread(target=send_mail, args=[subject, msg, sender, to])
t.start()

它应该可以在GAE上使用,但是在我上载应用程序并尝试执行它之后,会出现以下错误:
Django Version: 1.3.1
Exception Type: FrontendsNotSupported
Exception Location: /python27_runtime/python27_lib/versions/1/google/appengine/api/background_thread/background_thread.py in start_new_background_thread, line 84
Python Executable: /python27_runtime/python27_dist/python
Python Version: 2.7.3

有人知道为什么会出现此错误吗?

使用普通线程的代码:
from threading import Thread
from django.core.mail import send_mail

subject = 'Hello!'
msg = '\n \n Hello World!'
sender = settings.DEFAULT_FROM_EMAIL
to = 'xx@xx.com

t = Thread(target=send_mail, args=[subject, msg, sender, to], kwargs={'fail_silently': False})
t.setDaemon(True)
t.start()

最佳答案

您只能在backend实例上运行后台线程。

https://developers.google.com/appengine/docs/python/backends/

Backends are special App Engine instances that have no request deadlines, higher memory and CPU limits, and persistent state across requests. They are started automatically by App Engine and can run continously for long periods. Each backend instance has a unique URL to use for requests, and you can load-balance requests across multiple instances.



您可以在前端实例上使用普通线程(线程),但只能在后端上使用 backgroundthread

Code running on a backend can start a background thread, a thread that can "outlive" the request that spawns it. They allow backend instances to perform arbitrary periodic or scheduled tasks or to continue working in the background after a request has returned to the user.



https://developers.google.com/appengine/docs/python/backends/overview#background_threads

它实际上在错误 FrontendsNotSupported中说,因此不支持前端。但是,您可以在前端使用“常规” python线程语法。

关于multithreading - 使用Python 2.7的GAE上的线程引发 “FrontendsNotSupported”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com