Skip to main content
 首页 » 编程设计

python之App Engine 后端配置 (python)

2025年05月04日22mfryf

当我的 GAE 服务器尝试将大文件发送到 EC2 REST 服务器时,我遇到了 timeout 错误。我发现 Backends Python API 是我示例的一个很好的解决方案,但我在配置它时遇到了一些问题。

按照一些说明,我在我的项目文件夹中添加了一个简单的 backends.yaml。但是我仍然收到以下错误,这似乎是我创建后端实例失败。

  File "\Google\google_appengine\google\appengine\api\background_thread\background_thread.py", line 84, in start_new_background_thread 
    raise ERROR_MAP[error.application_error](error.error_detail) 
FrontendsNotSupported 

下面是我的代码,我的问题是:

  1. 目前,我在 OutputPage.py 中遇到超时错误,如何让此脚本在后端实例上运行?

更新

按照 Jimmy Kane 的建议,我为后端实例创建了一个新脚本 przm_batchmodel_backend.py。在启动我的 GAE 之后,现在我有两个端口(一个默认端口和一个后端端口)在运行我的站点。对吗?

应用.yaml

- url: /backend.html 
  script: przm_batchmodel.py 

后端.yaml

backends: 
- name: mybackend 
  class: B1 
  instances: 1 
  options: dynamic 

输出页面.py

from przm import przm_batchmodel 
from google.appengine.api import background_thread 
 
    class OutputPage(webapp.RequestHandler): 
        def post(self): 
            form = cgi.FieldStorage() 
            thefile = form['upfile'] 
 
            #this is the old way to initiate calculations 
            #html= przm_batchmodel.loop_html(thefile)   
 
            przm_batchoutput_backend.przmBatchOutputPageBackend(thefile) 
 
            self.response.out.write(html) 
    app = webapp.WSGIApplication([('/.*', OutputPage)], debug=True) 

przm_batchmodel.py

    def loop_html(thefile): 
        #parses uploaded csv and send its info. to the REST server, the returned value is a html page.  
        data= csv.reader(thefile.file.read().splitlines()) 
        response = urlfetch.fetch(url=REST_server, payload=data, method=urlfetch.POST, headers=http_headers, deadline=60)    
        return response 

przm_batchmodel_backend.py

    class BakendHandler(webapp.RequestHandler): 
        def post(self): 
            t = background_thread.BackgroundThread(target=przm_batchmodel.loop_html, args=[thefile]) 
            t.start() 
    app = webapp.WSGIApplication([('/backend.html', BakendHandler)], debug=True) 

请您参考如下方法:

您需要为后端工作创建一个应用程序"file"/脚本。就像您对 main 所做的一样。

所以像这样:

应用.yaml

- url: /backend.html 
  script: przm_batchmodel.py 

和 przm_batchmodel.py

class BakendHandler(webapp.RequestHandler): 
    def post(self): 
 
        html = 'test' 
        self.response.out.write(html) 
app = webapp.WSGIApplication([('/backend.html', OutputPage)], debug=True) 

我还可以建议使用新功能 modules哪些更容易设置?

编辑由于评论

可能设置不是您的问题。

来自docs

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.

您只能在后端使用backgroundthread。

于是再次编辑。移动代码的一部分:

 t = background_thread.BackgroundThread(target=przm_batchmodel.loop_html, args=[thefile]) 
 t.start() 
 self.response.out.write(html) 

到后端应用