【发布时间】:2016-07-28 19:16:13
【问题描述】:
我试图在多个线程中运行多个 IOLoop,我想知道 IOLoop 是如何工作的。
class WebThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self, name='WebThread')
def run(self):
curdir = os.path.dirname(os.path.realpath(__file__))
application = Application() #Very simple tornado.web.Application
http_server_api = tornado.httpserver.HTTPServer(application)
http_server_api.listen(8888)
logging.info('Starting application')
#tornado.ioloop.IOLoop.instance() is singleton, not for thread, right?
ioloop = tornado.ioloop.IOLoop()
ioloop.make_current()
ioloop.start()
根据文档,我不能使用 IOLoop.instance() 因为它是一个单例并且我正在一个线程中工作。所以我创建了自己的 IOLoop。但是这段代码监听了 8888 端口,却无法渲染任何网页。我想知道是否遗漏了什么,或者我是否需要以某种方式将 http_server 绑定到 IOLoop?
另外,我发现删除最后 3 行并替换为 tornado.ioloop.IOLoop.instance().start 非常适合单线程。但是单例和自创的 IOLoop 有什么区别呢?
我是 Tornado 的新手,欢迎任何答案。
【问题讨论】:
标签: python multithreading tornado