Commit 715d5090 by Sheng

ssh_connect wrapped to make it non-blocking

parent c549a01b
...@@ -2,13 +2,16 @@ import io ...@@ -2,13 +2,16 @@ import io
import logging import logging
import os.path import os.path
import socket import socket
import threading
import traceback import traceback
import uuid import uuid
import weakref import weakref
import paramiko import paramiko
import tornado.gen
import tornado.ioloop import tornado.ioloop
import tornado.web import tornado.web
import tornado.websocket import tornado.websocket
from tornado.concurrent import Future
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.iostream import _ERRNO_CONNRESET from tornado.iostream import _ERRNO_CONNRESET
from tornado.options import define, options, parse_command_line from tornado.options import define, options, parse_command_line
...@@ -222,19 +225,34 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler): ...@@ -222,19 +225,34 @@ class IndexHandler(MixinHandler, tornado.web.RequestHandler):
IOLoop.current().call_later(DELAY, recycle, worker) IOLoop.current().call_later(DELAY, recycle, worker)
return 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): def get(self):
self.render('index.html') self.render('index.html')
@tornado.gen.coroutine
def post(self): def post(self):
worker_id = None worker_id = None
status = None status = None
future = Future()
try: try:
worker = self.ssh_connect() threading.Thread(
except Exception as e: target=self.ssh_connect_wrapped, args=(future,)
).start()
yield future
except Exception as exc:
logging.error(traceback.format_exc()) logging.error(traceback.format_exc())
status = str(e) status = str(exc)
else: else:
worker = future.result()
worker.src_addr = self.get_client_addr() worker.src_addr = self.get_client_addr()
worker_id = worker.id worker_id = worker.id
workers[worker_id] = worker workers[worker_id] = worker
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment