我已经阅读了关于如何运行N个Tornado进程的各种文章和教程,其中N=number of core。我的代码正在工作,在所有16个内核上运行,但不知何故我设法搞砸了,我需要新的视角来看待这一点。
import tornado.ioloop
import tornado.web
import tornado.httpserver
from core import settings
from core import coreService
import services
from tornado.options import define, options, parse_command_line
define("port", default=settings.SERVER_PORT, help="run on the given port", type=int)
app = tornado.web.Application([
(r'/upload', coreService.Upload)
])
if __name__ == "__main__":
tornado.options.parse_command_line()
server = tornado.httpserver.HTTPServer(app, max_buffer_size=1024*1024*201)
server.bind(options.port)
# autodetect cpu cores and fork one process per core
server.start(0)
try:
print 'running on port %s' % options.port
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
tornado.ioloop.IOLoop.instance().stop()下面的代码抛出这个错误:
File "/opt/tornado/tornado/process.py", line 112, in fork_processes
raise RuntimeError("Cannot run in multiple processes: IOLoop instance "
RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()我就是看不出来。谢谢
:编辑:
正如Ben所说,我的方法之一就是给我带来麻烦。这是该方法的代码,有些人可能会从中受益:
from tornado import gen
import motor
db = motor.MotorClient().open_sync().proba
class Upload(BaseHandler):
@gen.engine
def post(self):
fs = yield motor.Op(motor.MotorGridFS(db).open)
gridin = yield motor.Op(fs.new_file)
yield motor.Op(gridin.write, 'First part\n')
yield motor.Op(gridin.write, 'Second part')
yield motor.Op(gridin.close)
print gridin._id
self.write(str(gridin._id))
self.finish()编辑2
我已经找到了我问题的最终解决方案。正如Ben所指出的,上面的方法给我带来了麻烦。电机文档中记录了在Tornado应用中包含电机的正确方法。下面是一个适用于我的except:
if __name__ == "__main__":
tornado.options.parse_command_line()
try:
server = tornado.httpserver.HTTPServer(app, max_buffer_size=1024*1024*201)
server.bind(8888)
server.start(0) # autodetect cpu cores and fork one process per core
db = motor.MotorClient().open_sync().proba
print 'running on port %s' % options.port
# Delayed initialization of settings
app.settings['db'] = db # from this point on db is available as self.settings['db']
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
tornado.ioloop.IOLoop.instance().stop()发布于 2014-03-26 10:17:34
如果我注释掉"core“和"services”导入,它就能正常工作。这些模块中的某个模块必须初始化单例事件循环(可能是间接的,例如,通过创建全局AsyncHTTPClient实例)。此警告是为了防止在父进程中创建的这些对象在子进程中不起作用这一事实。您必须找到创建这些对象的位置(不幸的是,没有好的工具来实现这一点),并将它们移到fork之后,以便在子进程中创建它们。
发布于 2014-11-18 23:14:36
当调试模式为tornado.web.Application时,将引发此异常。
application = tornado.web.Application([
(r"/", hello),
],
debug=False)将debug设置为False以解决此问题。
您可以启动多个进程来侦听每个端口:
server = tornado.httpserver.HTTPServer(application)
server.bind(1234) # port
server.start(4)
tornado.ioloop.IOLoop.instance().start()发布于 2016-03-18 03:26:11
我甚至在设置debug=False和autoreload=False时也遇到了同样的问题
我的目标是在TornadoApp.__init__()上创建一个实例的MotorClient
注释motor_client = ...行解决了我的问题。
class MainApp(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", views.HomeHandler),
(r".*", views.NotFoundHandler),
]
motor_client = motor.motor_tornado.MotorClient('mongodb://localhost:27017') 不幸的是,使用MotorClient迫使我放弃使用多进程运行Tornado。
https://stackoverflow.com/questions/22641015
复制相似问题