如何在 python 3 Google App Engine 标准环境中进行后台工作?

How can I do background work in python 3 Google App Engine standard environment?

我的 Appengine 应用程序 (/start) 上有一个端点,用于启动进程。在 python 2 环境中,我使用 deferred 到 运行 请求上下文之外的进程。在 appengine flex 上,您可以 run background threads that live outside of the request environment. At first look, I thought this was also true 的 python 3 标准环境,但仔细观察它包含以下注释:

However note that new threads or processes may not run after the inbound request is served.

这是否意味着一旦请求被处理,我启动的后台线程就会被杀死?如果是这样,开始后台工作的最佳方式是什么?

我可以通过在云任务中创建队列并 pickle 一切来重新创建 deferred 库,但这是最后的手段。

来自 Task Queue section of the Understanding differences between Python 2 and Python 3 on the App Engine standard environment 指南:

You can use Cloud Tasks (beta) to access the Task Queue service.

In cases where pull queues are appropriate, such as queuing up tasks or messages that will be pulled and processed by separate workers, Cloud Pub/Sub can be a good alternative. Cloud Pub/Sub offers similar functionality and delivery guarantees.

是的,云任务是推荐的解决方案。

但您实际上并不需要重新创建 deferred 库 - 该库建立在推送任务队列之上。 deferred 库相对于推送任务的唯一优势是您不必为任务预先注册处理程序(您只需传递要执行的函数及其参数)。但云任务并非如此 - 它们确实需要 GAE handlers.