From c822c2df9c8b7e48faeb83512c9a8117d0d27555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Wed, 24 Aug 2016 00:22:03 +0200 Subject: [PATCH 1/2] Make misp-modules really asynchronous --- misp_modules/__init__.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/misp_modules/__init__.py b/misp_modules/__init__.py index 994c742..fd0d740 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(): From 1034f73479bed86f069c5411de13a7de92249177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Wed, 24 Aug 2016 10:01:44 +0200 Subject: [PATCH 2/2] Fix python 3.3 and 3.4 --- misp_modules/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/misp_modules/__init__.py b/misp_modules/__init__.py index fd0d740..81c92b1 100644 --- a/misp_modules/__init__.py +++ b/misp_modules/__init__.py @@ -170,7 +170,11 @@ class ListModules(tornado.web.RequestHandler): class QueryModule(tornado.web.RequestHandler): - executor = ThreadPoolExecutor(None) + try: + # Python 3.5, use as many threads as possible + executor = ThreadPoolExecutor() + except: + executor = ThreadPoolExecutor(5) @run_on_executor def run_request(self, jsonpayload):