Python 如何使用CherryPy组织多个应用服务器?
我正在用CherryPy开发多个Web服务。其中一些必须使用相同的代码部分,因此我希望避免代码重复 我希望CherryPy Web服务器能够为所有安装在不同URL上的应用程序提供服务 这就是我组织文件层次结构的方式(当然是简化版本): 假设server.py是入口点。它导入每个app.py并从中获取一个WSGI应用程序。然后,它会把它们装上以供食用 我希望能够从common/导入一些类,但不需要多次修改sys.path。因此,我认为我可以在ext中导入所需的模块。当我在应用程序中需要它们时,我只需进行如下导入:Python 如何使用CherryPy组织多个应用服务器?,python,python-2.7,cherrypy,Python,Python 2.7,Cherrypy,我正在用CherryPy开发多个Web服务。其中一些必须使用相同的代码部分,因此我希望避免代码重复 我希望CherryPy Web服务器能够为所有安装在不同URL上的应用程序提供服务 这就是我组织文件层次结构的方式(当然是简化版本): 假设server.py是入口点。它导入每个app.py并从中获取一个WSGI应用程序。然后,它会把它们装上以供食用 我希望能够从common/导入一些类,但不需要多次修改sys.path。因此,我认为我可以在ext中导入所需的模块。当我在应用程序中需要它们时,我只
from ext.share_module import class
1-您认为文件层次结构好吗
2-我希望每个应用程序都独立于服务器和其他应用程序。因此,对于导入,我希望能够简单地考虑APP1//是根文件夹,而不是应用程序/。但是因为server.py是入口点,所以我不能像我想要的那样导入模块
3-任何建议、考虑或建议?:) 好的,这是我最后的解决方案。 这几乎与问题中的相同
apps/
server.py -- runs the WSGI Server
app1/
api/ -- contains the business logic
controller/ -- makes the interface between HTTP and the API
lib/
model/
app.py -- provides a function get_wsgi_app(name, config, app_instance_name)
url.py -- like django, makes a binding between urls and controller
app2/
-- same as app1
common/ -- All common classes and modules. Folders are the same that apps.
api/
controller/
lib/
model/
configs/
app1.cfg
app2.cfg
server.cfg
它是如何工作的:
server.py是入口点。他的角色是启动WSGI服务器并装载WSGI应用程序。
py实例化应用程序并加载其配置。(您可以有多个app1实例)
py包含应用程序的URL
当我需要导入一个模块时,我只使用从app文件夹开始的绝对导入
例:
我从未尝试过使用相对导入,因为我更喜欢显式导入,尤其是在文件夹名称通常相同的情况下 我打算问完全相同的问题。如果你能通过描述你最终做了什么以及为什么来回答你自己的问题,我将不胜感激。好的,没问题,我会尽快回答,可能是明天。好的,我回答了,如果你不想了解更多细节,请不要犹豫。谢谢你的回答!
apps/
server.py -- runs the WSGI Server
app1/
api/ -- contains the business logic
controller/ -- makes the interface between HTTP and the API
lib/
model/
app.py -- provides a function get_wsgi_app(name, config, app_instance_name)
url.py -- like django, makes a binding between urls and controller
app2/
-- same as app1
common/ -- All common classes and modules. Folders are the same that apps.
api/
controller/
lib/
model/
configs/
app1.cfg
app2.cfg
server.cfg
from app1.api.my_module import API
from common.api.login_api import LoginAPI
class Foo():
...