【问题标题】:Can I use background threads on Google App Engine Standard + Python 3?我可以在 Google App Engine Standard + Python 3 上使用后台线程吗?
【发布时间】:2021-06-17 10:15:08
【问题描述】:

GAE documentation 在标准环境中对后台线程的支持含糊不清:

这是我尝试运行但没有成功的示例代码。我试图弄清楚问题是出在代码中还是不受支持。

from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=5)
executor.submit(my_func, *args, **kwargs)

【问题讨论】:

    标签: python multithreading google-app-engine


    【解决方案1】:

    尝试添加.result() 以便您的HTTP 请求阻塞直到my_func() 完成?例如:

    future = executor.submit(my_func, *args, **kwargs)
    future.result()
    

    Circa 2014, back in the original Python 2 runtime, App Engine 区分“后台”线程,它可以在 HTTP 请求完成后继续运行,而“普通”线程则不能。我不知道当前 Python 3 运行时是否/如何更改,但它可能仍会在请求完成时切断“正常”线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 2015-02-15
      • 1970-01-01
      • 2012-10-25
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多