Python 港口龙卷风在gunicorn的应用
我正在运行一个带有Tornado应用程序的Python api服务器。 我的Tornado应用程序使用了Python 港口龙卷风在gunicorn的应用,python,python-3.x,tornado,gunicorn,Python,Python 3.x,Tornado,Gunicorn,我正在运行一个带有Tornado应用程序的Python api服务器。 我的Tornado应用程序使用了Tornado.web,定义如下 from tornado import web def application(): handlers = [ # apis (r'/api/1', th.APIHandler1), (r'/api/2', th.APIHandler2) ] settings = dict()
Tornado.web
,定义如下
from tornado import web
def application():
handlers = [
# apis
(r'/api/1', th.APIHandler1),
(r'/api/2', th.APIHandler2)
]
settings = dict()
return web.Application(handlers, **settings)
所以它在IOLoop中运行就像
app=application()
应用程序侦听(内部(端口))
ioloop.ioloop.instance().start()
因此,它不是一个WSGI应用程序。处理程序通常使用@tornado.web.asynchronous
模式进行修饰,对于cpu密集型任务,使用@tornado.gen.coroutine
模式进行修饰,或者在某些特定情况下,对于长时间任务,使用@tornado.concurrent.run\u executor
修饰器作为线程池中的线程运行。在这个特定的例子中,我使用了一个类似的:
我想将其移植到gunicorn
以利用fork workers之前的方法。
我调查过的一个可能的解决方案是gunicorn,它应该看起来像:
from routes import Mapper
from test import app as apiHandler1
from test import app as apiHandler2
class Application(object):
def __init__(self):
self.map = Mapper()
self.map.connect('app1', '/api/1', app=apiHandler1)
self.map.connect('app2', '/api/2', app=apiHandler2)
def __call__(self, environ, start_response):
match = self.map.routematch(environ=environ)
if not match:
return self.error404(environ, start_response)
return match[0]['app'](environ, start_response)
app = Application()
没有关于将Tornado异步应用程序移植到gunicorn wsgi应用程序的具体文档,因此我的问题是这种方法是否正确。Tornado不是基于wsgi的框架,因此不能在wsgi应用程序服务器中使用(在旧版本的Tornado中,可以在WSGI兼容模式下运行Tornado应用程序的一个子集,但在Tornado 6.0中删除了该子集)。Gunicorn有一个特定于Tornado的应用程序,必须改用。是否有任何示例代码/视频用于使用Gunicorn的Tornado的worker_类。谢谢
from routes import Mapper
from test import app as apiHandler1
from test import app as apiHandler2
class Application(object):
def __init__(self):
self.map = Mapper()
self.map.connect('app1', '/api/1', app=apiHandler1)
self.map.connect('app2', '/api/2', app=apiHandler2)
def __call__(self, environ, start_response):
match = self.map.routematch(environ=environ)
if not match:
return self.error404(environ, start_response)
return match[0]['app'](environ, start_response)
app = Application()