终于找到一种 比较方便的实现 tornado 异步操作且支持多线程并发的方法了
server.py
from tornado import gen, web, ioloop, template
from tornado.gen import coroutine, Future
from tornado.ioloop import IOLoop
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
import threading
import time
class Pic_Pred(web.RequestHandler):
#线程池
max_thread_num = 1
executor = ThreadPoolExecutor(max_workers=max_thread_num)
# 让接口保持长连接
@web.asynchronous
@gen.engine
def post(self):
try:
body_dic = json.loads(self.request.body.decode('utf-8'))
imgb64 = body_dic.get("img_b64", None)
if imgb64 is None:
# 不可直接 return, 必须分开
yield self.finish({"code":1, "result":[], "msg":"error: no image, please check !!!"})
return
buf_str