Skip to content

Commit e54d9e6

Browse files
authored
Updated file upload process to be multi-threaded (#28)
Closes #1
1 parent a4097b5 commit e54d9e6

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

app.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
import threading
2727
from tornado import httpserver, ioloop, web
2828
from tornado.options import define, options, parse_command_line
29+
try:
30+
import Queue as queue
31+
except ImportError:
32+
import queue
2933

3034
# Command Line Options
3135
define("port", default=8088, help="Port the web app will run on")
@@ -72,6 +76,9 @@ def delete(self):
7276
class UploadHandler(web.RequestHandler):
7377
def post(self):
7478
finish_ret = []
79+
threads = []
80+
ret_queue = queue.Queue()
81+
7582
new_files = self.request.files['file']
7683
for file_des in new_files:
7784
file_name = temp_img_prefix + file_des['filename']
@@ -80,18 +87,33 @@ def post(self):
8087
output_file = open(rel_path, 'wb')
8188
output_file.write(file_des['body'])
8289
output_file.close()
83-
caption = run_ml(rel_path)
84-
finish_ret.append({
85-
"file_name": rel_path,
86-
"caption": caption[0]['caption']
87-
})
90+
t = threading.Thread(target=run_ml_queued,
91+
args=(rel_path, ret_queue))
92+
threads.append(t)
93+
t.start()
94+
95+
for t in threads:
96+
t.join()
97+
98+
sorted_ret = sorted(list(ret_queue.queue), key=lambda t: t[0].lower())
99+
for rel_path, caption in sorted_ret:
100+
finish_ret.append({
101+
"file_name": rel_path,
102+
"caption": caption[0]['caption']
103+
})
104+
88105
if not finish_ret:
89106
self.send_error(400)
90107
return
91108
sort_image_captions()
92109
self.finish(json.dumps(finish_ret))
93110

94111

112+
def run_ml_queued(img_path, ret_queue):
113+
caption = run_ml(img_path)
114+
ret_queue.put((img_path, caption))
115+
116+
95117
def valid_file_ext(filename):
96118
return '.' in filename and filename.split('.', 1)[1].lower() in VALID_EXT
97119

@@ -127,8 +149,6 @@ def prepare_metadata():
127149
for img in rel_img_list:
128150
t = threading.Thread(target=run_ml, args=(img,))
129151
threads.append(t)
130-
131-
for t in threads:
132152
t.start()
133153

134154
for t in threads:

0 commit comments

Comments
 (0)