commit 715d50907ab7e368ca2cd71e2b0be2e8636b7946
parent c549a01b1d4b71694669d2be92ea84826eae51df
Author: Sheng <webmaster0115@gmail.com>
Date: Mon, 2 Apr 2018 10:25:49 +0800
ssh_connect wrapped to make it non-blocking
Diffstat:
M | main.py | | | 24 | +++++++++++++++++++++--- |
1 file changed, 21 insertions(+), 3 deletions(-)
diff --git a/main.py b/main.py
@@ -2,13 +2,16 @@ import io
import logging
import os.path
import socket
+import threading
import traceback
import uuid
import weakref
import paramiko
+import tornado.gen
import tornado.ioloop
import tornado.web
import tornado.websocket
+from tornado.concurrent import Future
from tornado.ioloop import IOLoop
from tornado.iostream import _ERRNO_CONNRESET
from tornado.options import define, options, parse_command_line
@@ -222,19 +225,34 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
IOLoop.current().call_later(DELAY, recycle, worker)
return worker
+ def ssh_connect_wrapped(self, future):
+ try:
+ worker = self.ssh_connect()
+ except Exception as exc:
+ future.set_exception(exc)
+ raise exc
+ else:
+ future.set_result(worker)
+
def get(self):
self.render('index.html')
+ @tornado.gen.coroutine
def post(self):
worker_id = None
status = None
+ future = Future()
try:
- worker = self.ssh_connect()
- except Exception as e:
+ threading.Thread(
+ target=self.ssh_connect_wrapped, args=(future,)
+ ).start()
+ yield future
+ except Exception as exc:
logging.error(traceback.format_exc())
- status = str(e)
+ status = str(exc)
else:
+ worker = future.result()
worker.src_addr = self.get_client_addr()
worker_id = worker.id
workers[worker_id] = worker