[英]How to use Python main() function in GAE (Google App Engine)?
我想在我的GAE代码中使用main()
函数
(注意:下面的代码只是一个更大程序的最小演示,因此需要main()
)。
如果我使用以下代码,它将按预期执行:
import webapp2
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
我的app.yaml
在哪里:
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
但是 ,我无法弄清楚如何实现main()
函数,并且仍然具有顶部代码中的app
行为。 即,以下内容:
# with main()
import webapp2
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
def main():
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
if __name__ == '__main__':
main()
对于http://localhost:8080/get:
给出以下错误http://localhost:8080/get:
$ dev_appserver.py .
INFO 2016-10-17 11:29:30,962 devappserver2.py:769] Skipping SDK update check.
INFO 2016-10-17 11:29:31,059 api_server.py:205] Starting API server at: http://localhost:45865
INFO 2016-10-17 11:29:31,069 dispatcher.py:197] Starting module "default" running at: http://localhost:8080
INFO 2016-10-17 11:29:31,073 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2016-10-17 11:29:37,461 wsgi.py:263]
Traceback (most recent call last):
File "/home/.../sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/.../sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 302, in _LoadHandler
raise err
ImportError: <module 'main' from '/home/.../main.pyc'> has no attribute app
INFO 2016-10-17 11:29:37,496 module.py:788] default: "GET /get HTTP/1.1" 500 -
试:
# with main()
import webapp2
app = webapp2.RequestHandler()
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
def main():
global app
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
return app
if __name__ == '__main__':
app = main()
结果是:
INFO 2016-10-17 12:30:34,751 module.py:402] [default] Detected file changes:
/home/openstack/googleAppEngine/fastsimon/task2/task2_with_main/main.py
ERROR 2016-10-17 12:30:42,328 wsgi.py:279]
Traceback (most recent call last):
File "/home/openstack/googleAppEngine/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
TypeError: 'RequestHandler' object is not callable
INFO 2016-10-17 12:30:42,335 module.py:788] default: "GET /get HTTP/1.1" 500 -
GAE应用程序并非设计为独立的应用程序, main()
函数对它们没有多大意义。
基本上,GAE应用程序实际上只是处理程序代码和规则/配置的集合,旨在扩展和自定义通用GAE红外线/沙盒代码的行为,以便其表现您的应用程序。 您可以从回溯中看到-其他代码正在调用您的处理程序代码(到达代码之前的堆栈可能要深得多)。
在您的特定情况下, app
变量必须在main.py
是全局变量,以匹配script: main.app
app.yaml
配置文件中的script: main.app
config行。 这就是追溯的目的。
至于组织大型应用程序的代码,还有其他方法可以实现:
将应用分为多个模块/服务 ,每个模块/服务都有自己的app.yaml
配置文件。 例如: 就文件夹结构而言,Google App Engine应用程序中的默认服务/模块是否可以是非默认服务/模块的兄弟?
将服务/模块拆分为多个“脚本”-类似于main.py
文件的app.yaml
文件中的主要入口点,每个入口点都有自己的app
main.py
实际上,它们只是路由和处理程序之间的映射器。 例如: App Engine会为除root以外的任何路径抛出404 Not Found
使用webapp2的延迟加载处理程序技术将一个app
映射程序的处理程序拆分为多个文件。 例子:
在极端情况下, main.py
文件可能只包含app
变量-这实际上是唯一的要求。
似乎解决方案非常简单(它一直躲藏在我眼中,因为它被隐藏了): __name__
是main而不是__main__ !
简而言之,以下代码按预期利用了main()
:
# with main()
import webapp2
class GetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in GET')
class SetHandler(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('in SET')
def main():
global app
app = webapp2.WSGIApplication([
('/get', GetHandler),
('/set', SetHandler),
], debug=True)
# Note that it's 'main' and not '__main__'
if __name__ == 'main':
main()
https://webapp2.readthedocs.io/zh-CN/latest/tutorials/quickstart.nogae.html描述了如何在GAE环境之外使用GAE应用程序:
from myapp import app
def main():
from paste import httpserver
httpserver.serve(app, host='localhost', port='8070')
if __name__ == '__main__':
main()
我这样做是为了使用Pycharm IDE调试应用程序,而不是从dev_appserver命令窗口进行调试。 它工作正常。 我将结果与运行在端口8080上的dev_appserver和运行在8070上的调试器进行了比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.