file_manager: process copy requests in another thread

The copy methods are blocking and will block the asyncio event loop.  Run them in a  ThreadPoolExecutor to keep the event loop free.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-22 06:58:29 -04:00 committed by Eric Callahan
parent 43fcca5ffa
commit 93fcd1ae86
1 changed files with 14 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import shutil
import logging import logging
import json import json
import tempfile import tempfile
from concurrent.futures import ThreadPoolExecutor
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.locks import Event from tornado.locks import Event
from inotify_simple import INotify from inotify_simple import INotify
@ -263,15 +264,21 @@ class FileManager:
except Exception as e: except Exception as e:
raise self.server.error(str(e)) raise self.server.error(str(e))
elif ep == "/server/files/copy": elif ep == "/server/files/copy":
try: ioloop = IOLoop.current()
if os.path.isdir(source_path): with ThreadPoolExecutor(max_workers=1) as tpe:
shutil.copytree(source_path, dest_path) await ioloop.run_in_executor(
else: tpe, self._do_copy, source_path, dest_path)
shutil.copy2(source_path, dest_path)
except Exception as e:
raise self.server.error(str(e))
return "ok" return "ok"
def _do_copy(self, source_path, dest_path):
try:
if os.path.isdir(source_path):
shutil.copytree(source_path, dest_path)
else:
shutil.copy2(source_path, dest_path)
except Exception as e:
raise self.server.error(str(e))
def _list_directory(self, path, is_extended=False): def _list_directory(self, path, is_extended=False):
if not os.path.isdir(path): if not os.path.isdir(path):
raise self.server.error( raise self.server.error(