运行 在多进程模式下使用 aiomysql 的龙卷风
run tornado with aiomysql in multi-process mode
我一直在尝试 运行 使用以下代码在多进程模式下使用 aiomysql 龙卷风
@asyncio.coroutine
def get_mysql_connection(loop):
return (yield from aiomysql.create_pool(host=host,port=3306,user=user, password=pass, db=db, loop=loop))
if __name__ == "__main__":
tornado.platform.asyncio.AsyncIOMainLoop().install()
ioloop = asyncio.get_event_loop()
mysql = ioloop.run_until_complete(get_mysql_connection(ioloop))
options.parse_config_file("app.conf")
app = make_app(mysql)
print('listening on %s:%s...' %(options.host, options.port))
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
server.start(0) #this is my problem
ioloop.run_forever()
但我一直收到以下错误
RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()
除 ioloop.start(0)
行外一切正常,是否可以使 aiomysql
和 tornado
这两个库在多进程模式下很好地工作?如果没有,我的其他选择是什么
龙卷风版本 4.4.2
python 版本 3.6.0
aiomysql 版本 0.0.9
如消息所述,如果您在 IOLoop 初始化之前执行此操作,则只能分叉工作进程。这对于 AsyncIOMainLoop 来说有点微妙,因为您想尽早安装它。解决方案是按以下顺序启动您的程序:
tornado.options.parse_config_file(...)
socks = tornado.netutil.bind_sockets(options.port, options.host)
tornado.process.fork_processes(0)
tornado.asyncio.AsyncIOMainLoop().install()
# Initialize the rest of your app, create the HTTPServer,
# and instead of listen() or start(), do
server.add_sockets(socks)
what are my other options
在我看来,最好使用像 supervisord
这样的外部进程管理器,而不是在您的应用中分叉多个进程。这避免了大多数这些初始化顺序陷阱。
我一直在尝试 运行 使用以下代码在多进程模式下使用 aiomysql 龙卷风
@asyncio.coroutine
def get_mysql_connection(loop):
return (yield from aiomysql.create_pool(host=host,port=3306,user=user, password=pass, db=db, loop=loop))
if __name__ == "__main__":
tornado.platform.asyncio.AsyncIOMainLoop().install()
ioloop = asyncio.get_event_loop()
mysql = ioloop.run_until_complete(get_mysql_connection(ioloop))
options.parse_config_file("app.conf")
app = make_app(mysql)
print('listening on %s:%s...' %(options.host, options.port))
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
server.start(0) #this is my problem
ioloop.run_forever()
但我一直收到以下错误
RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()
除 ioloop.start(0)
行外一切正常,是否可以使 aiomysql
和 tornado
这两个库在多进程模式下很好地工作?如果没有,我的其他选择是什么
龙卷风版本 4.4.2
python 版本 3.6.0
aiomysql 版本 0.0.9
如消息所述,如果您在 IOLoop 初始化之前执行此操作,则只能分叉工作进程。这对于 AsyncIOMainLoop 来说有点微妙,因为您想尽早安装它。解决方案是按以下顺序启动您的程序:
tornado.options.parse_config_file(...)
socks = tornado.netutil.bind_sockets(options.port, options.host)
tornado.process.fork_processes(0)
tornado.asyncio.AsyncIOMainLoop().install()
# Initialize the rest of your app, create the HTTPServer,
# and instead of listen() or start(), do
server.add_sockets(socks)
what are my other options
在我看来,最好使用像 supervisord
这样的外部进程管理器,而不是在您的应用中分叉多个进程。这避免了大多数这些初始化顺序陷阱。