我正在构建一个测试服务器,当端点被命中时,它会加载一个巨大的pickle文件(花费大约30秒)。我的目标是在龙卷风Web服务器作为单独的线程启动时更新它以将pickle作为python对象加载到后台的内存中。因此,当端点被命中时,它要么在内存中找到它,要么等待线程完成加载。这样可以使启动速度更快。
我在这里寻求一些建议,了解添加异步的最佳方法,以使此操作正常运行。
my_server.py
import tornado.ioloop
import tornado.web
from my_class import MyClass
class MainHandler(tornado.web.RequestHandler):
def get(self):
m = MyClass.get_foobar_object_by_name('foobar')
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
MyClass.load() # takes 30s to load
tornado.ioloop.IOLoop.current().start()
my_class.py
class MyClass(object):
pickle_path = '/opt/some/path/big_file.pickle'
foobar_map = None
@staticmethod
def load():
# this step takes about 30s to load
MyClass.foobar_map = pickle.load(open(local_path, 'rb'))
@staticmethod
def get_foobar_object_by_name(foobar_name):
if MyClass.foobar_map is None:
MyClass.load()
return MyClass.foobar_map.get(foobar_name)
答案 0 :(得分:2)
pickle
模块具有同步接口,因此异步运行它的唯一方法是在另一个线程上运行它。在Tornado 5.0中使用新的IOLoop.run_in_executor
接口:
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler
from tornado.locks import Lock
class MyClass:
lock = Lock()
@staticmethod
async def load():
async with MyClass.lock():
# Check again inside the lock to make sure we only do this once.
if MyClass.foobar_map is None:
MyClass.foobar_map = await IOLoop.current().run_in_executor(None, pickle.load, open(local_path, 'rb'))
@staticmethod
async def get_foobar_object_by_name(foobar_name):
if MyClass.foobar_map is None:
await MyClass.load()
return MyClass.foobar_map.get(foobar_name)
class MainHandler(RequestHandler):
async def get(self):
m = await MyClass.get_foobar_object_by_name('foobar')
self.write("Hello, world")
请注意async
具有传染性:调用async
函数的任何内容也需要async
并使用await
。