use ThreadPoolExecutor

This commit is contained in:
j 2019-10-13 11:16:45 +01:00
parent 0e11f04d44
commit 1e39fc48b2

View file

@ -17,7 +17,8 @@ import db
import settings
import tornado.web
import tornado.gen
import tornado.concurrent
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
from oxtornado import json_dumps, json_response
@ -27,6 +28,8 @@ import state
import logging
logger = logging.getLogger(__name__)
MAX_WORKERS = 4
class OptionalBasicAuthMixin(object):
class SendChallenge(Exception):
@ -177,6 +180,7 @@ class ReaderHandler(OMLHandler):
return serve_static(self, path, 'text/html')
class UploadHandler(OMLHandler):
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
def initialize(self, context=None):
self._context = context
@ -184,22 +188,14 @@ class UploadHandler(OMLHandler):
def get(self):
self.write('use POST')
@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self):
if 'origin' in self.request.headers and self.request.host not in self.request.headers['origin']:
logger.debug('reject cross site attempt to access api %s', self.request)
self.set_status(403)
self.write('')
return
def save_files(context, request, callback):
@run_on_executor
def save_files(self, request):
listname = request.arguments.get('list', None)
if listname:
listname = listname[0]
if isinstance(listname, bytes):
listname = listname.decode('utf-8')
with context():
with self._context():
prefs = settings.preferences
ids = []
for upload in request.files.get('files', []):
@ -240,13 +236,21 @@ class UploadHandler(OMLHandler):
add_record('edititem', item.id, item.meta)
item.update()
if listname and ids:
l = List.get(settings.USER_ID, listname)
if l:
l.add_items(ids)
list_ = List.get(settings.USER_ID, listname)
if list_:
list_.add_items(ids)
response = json_response({'ids': ids})
callback(response)
return response
response = yield tornado.gen.Task(save_files, self._context, self.request)
@tornado.gen.coroutine
def post(self):
if 'origin' in self.request.headers and self.request.host not in self.request.headers['origin']:
logger.debug('reject cross site attempt to access api %s', self.request)
self.set_status(403)
self.write('')
return
response = yield self.save_files(self.request)
if 'status' not in response:
response = json_response(response)
response = json_dumps(response)