diff --git a/misp_modules/__init__.py b/misp_modules/__init__.py index 994c7428..fd0d7400 100644 --- a/misp_modules/__init__.py +++ b/misp_modules/__init__.py @@ -22,8 +22,6 @@ import os import signal import sys -from tornado.ioloop import IOLoop -import tornado.web import importlib import json import logging @@ -31,6 +29,11 @@ import fnmatch import argparse import re +import tornado.web +from tornado.ioloop import IOLoop +from tornado.concurrent import run_on_executor +from concurrent.futures import ThreadPoolExecutor + try: from .modules import * HAS_PACKAGE_MODULES = True @@ -166,23 +169,23 @@ class ListModules(tornado.web.RequestHandler): self.write(json.dumps(ret)) -@tornado.gen.coroutine -def async_module(request, write_fct): - jsonpayload = request.body.decode('utf-8') - x = json.loads(jsonpayload) - log.debug('MISP QueryModule request {0}'.format(jsonpayload)) - ret = mhandlers[x['module']].handler(q=jsonpayload) - write_fct(json.dumps(ret)) - - class QueryModule(tornado.web.RequestHandler): + executor = ThreadPoolExecutor(None) + + @run_on_executor + def run_request(self, jsonpayload): + x = json.loads(jsonpayload) + log.debug('MISP QueryModule request {0}'.format(jsonpayload)) + return mhandlers[x['module']].handler(q=jsonpayload) + @tornado.gen.coroutine def post(self): - global mhandlers try: - yield async_module(self.request, self.write) + jsonpayload = self.request.body.decode('utf-8') + response = yield self.run_request(jsonpayload) + self.write(json.dumps(response)) except Exception: - log.exception("Someting bad happened.") + log.exception('Something went wrong:') def main():