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 settings
import tornado.web import tornado.web
import tornado.gen 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 from oxtornado import json_dumps, json_response
@ -27,6 +28,8 @@ import state
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
MAX_WORKERS = 4
class OptionalBasicAuthMixin(object): class OptionalBasicAuthMixin(object):
class SendChallenge(Exception): class SendChallenge(Exception):
@ -177,6 +180,7 @@ class ReaderHandler(OMLHandler):
return serve_static(self, path, 'text/html') return serve_static(self, path, 'text/html')
class UploadHandler(OMLHandler): class UploadHandler(OMLHandler):
executor = ThreadPoolExecutor(max_workers=MAX_WORKERS)
def initialize(self, context=None): def initialize(self, context=None):
self._context = context self._context = context
@ -184,22 +188,14 @@ class UploadHandler(OMLHandler):
def get(self): def get(self):
self.write('use POST') self.write('use POST')
@tornado.web.asynchronous @run_on_executor
@tornado.gen.coroutine def save_files(self, request):
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):
listname = request.arguments.get('list', None) listname = request.arguments.get('list', None)
if listname: if listname:
listname = listname[0] listname = listname[0]
if isinstance(listname, bytes): if isinstance(listname, bytes):
listname = listname.decode('utf-8') listname = listname.decode('utf-8')
with context(): with self._context():
prefs = settings.preferences prefs = settings.preferences
ids = [] ids = []
for upload in request.files.get('files', []): for upload in request.files.get('files', []):
@ -240,13 +236,21 @@ class UploadHandler(OMLHandler):
add_record('edititem', item.id, item.meta) add_record('edititem', item.id, item.meta)
item.update() item.update()
if listname and ids: if listname and ids:
l = List.get(settings.USER_ID, listname) list_ = List.get(settings.USER_ID, listname)
if l: if list_:
l.add_items(ids) list_.add_items(ids)
response = json_response({'ids': 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: if 'status' not in response:
response = json_response(response) response = json_response(response)
response = json_dumps(response) response = json_dumps(response)