Make controller async

This commit is contained in:
Bálint Bokros 2018-02-12 18:04:59 +01:00
parent 86aecd18a8
commit cb46a8b80e
4 changed files with 20 additions and 6 deletions

View File

@ -0,0 +1,14 @@
from tfw.networking.serialization import deserialize_all
from tfw.networking.solvable_connector import SolvableConnector
class AsyncSolvableConnector(SolvableConnector):
def __init__(self, async_zmq_context=None):
if async_zmq_context is None:
from zmq.eventloop.future import Context
async_zmq_context = Context.instance()
super(AsyncSolvableConnector, self).__init__(async_zmq_context)
async def recv(self):
response = await self._zmq_req_socket.recv_multipart()
return deserialize_all(*response)

View File

@ -4,13 +4,13 @@ from tornado.web import Application
from tao.config import taoenv from tao.config import taoenv
from tfw.config import tfwenv from tfw.config import tfwenv
from handlers import SolutionCheckHandler, TestHandler from handlers import SolutionCheckHandler, TestHandler
from tfw.networking.solvable_connector import SolvableConnector from tfw.networking.async_solvable_connector import AsyncSolvableConnector
from tfw.config.logs import logging from tfw.config.logs import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
if __name__ == '__main__': if __name__ == '__main__':
solvable_connector = SolvableConnector() solvable_connector = AsyncSolvableConnector()
routes = [ routes = [
(r'/{secret}/?'.format(secret=taoenv.SECRET), SolutionCheckHandler, {'solvable_connector': solvable_connector}), (r'/{secret}/?'.format(secret=taoenv.SECRET), SolutionCheckHandler, {'solvable_connector': solvable_connector}),
(r'/{secret}/test/?'.format(secret=taoenv.SECRET), TestHandler, {'solvable_connector': solvable_connector}) (r'/{secret}/test/?'.format(secret=taoenv.SECRET), TestHandler, {'solvable_connector': solvable_connector})

View File

@ -7,9 +7,9 @@ class SolutionCheckHandler(RequestHandler):
def initialize(self, solvable_connector): def initialize(self, solvable_connector):
self.solvable_connector = solvable_connector self.solvable_connector = solvable_connector
def get(self): async def get(self):
log.debug('Sending request to solvable') log.debug('Sending request to solvable')
self.solvable_connector.send('solution_check', {}) self.solvable_connector.send('solution_check', {})
resp_key, resp_data = self.solvable_connector.recv() resp_key, resp_data = await self.solvable_connector.recv()
log.debug('Received answer from solvable') log.debug('Received answer from solvable')
self.write(resp_data) self.write(resp_data)

View File

@ -5,7 +5,7 @@ class TestHandler(RequestHandler):
def initialize(self, solvable_connector): def initialize(self, solvable_connector):
self.solvable_connector = solvable_connector self.solvable_connector = solvable_connector
def get(self): async def get(self):
self.solvable_connector.send('test', {}) self.solvable_connector.send('test', {})
resp_key, resp_data = self.solvable_connector.recv() resp_key, resp_data = await self.solvable_connector.recv()
self.write(resp_data) self.write(resp_data)