use ThreadPoolExecutor

This commit is contained in:
j 2018-08-26 16:03:48 +02:00
commit ff7cee2be1
2 changed files with 50 additions and 83 deletions

View file

@ -8,8 +8,10 @@ import tornado.concurrent
import tornado.gen
import tornado.ioloop
import tornado.web
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
from oxtornado import run_async
from settings import icons_db_path, static_path
from utils import resize_image, is_svg
import db
@ -18,6 +20,9 @@ import db
import logging
logger = logging.getLogger(__name__)
MAX_WORKERS = 4
class Icons(dict):
def __init__(self, db):
@ -174,53 +179,20 @@ def get_icon_sync(id, type_, size):
data = ''
return data
@run_async
def get_icon(id, type_, size, callback):
callback(get_icon_sync(id, type_, size))
def clear_default_cover_cache():
icons.clear('default:cover:')
@run_async
def get_icon_app(id, type_, size, callback):
with db.session():
from item.models import Item
item = Item.get(id)
if not item:
data = ''
else:
if type_ == 'cover' and not item.meta.get('cover'):
type_ = 'preview'
if type_ == 'preview' and not item.files.count():
type_ = 'cover'
if size:
skey = '%s:%s:%s' % (type_, id, size)
key = '%s:%s' % (type_, id)
data = None
if size:
data = icons[skey]
if data:
size = None
if not data:
data = icons[key]
if not data:
data = icons.default_cover()
size = None
if size:
try:
data = resize_image(data, size=size)
icons[skey] = data
except:
pass
data = bytes(data) or ''
callback(data)
class IconHandler(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
def initialize(self):
pass
@tornado.web.asynchronous
@run_on_executor
def get_icon(self, id, type_, size):
return get_icon_sync(id, type_, size)
@tornado.gen.coroutine
def get(self, id, type_, size=None):
@ -232,7 +204,7 @@ class IconHandler(tornado.web.RequestHandler):
self.set_header('Content-Type', 'image/jpeg')
response = yield tornado.gen.Task(get_icon, id, type_, size)
response = yield self.get_icon(id, type_, size)
if not response:
self.set_status(404)
return