<commit_before>#!/usr/bin/env python
"""
The Tornado server used to receive operation requests and deliver results to the user.
"""
import json
import os

from sys import stderr
from linguine.transaction import Transaction
from concurrent.futures import ThreadPoolExecutor
from linguine.transaction_exception import TransactionException

"""
Check to ensure Tornado is installed
"""
try:
    import tornado.ioloop
    import tornado.web
except ImportError:
    sys.stderr.write("Tornado not found.")

class MainHandler(tornado.web.RequestHandler):
    numTransactionsRunning = 0
    try:
        maxThreadPoolWorkers = int(os.environ['LINGUINE_THREADS'])
    except KeyError as err:
        maxThreadPoolWorkers = 2
    print("starting thread pool with " + str(maxThreadPoolWorkers) + " threads.")
    analysis_executor = ThreadPoolExecutor(max_workers=maxThreadPoolWorkers)

    def post(self):
        self.set_header('Content-Type', 'application/json')
        try:
            self.numTransactionsRunning+=1
            transaction = Transaction()
            requestObj = transaction.parse_json(self.request.body)
            transaction.read_corpora(transaction.corpora_ids)
            transaction.calcETA(self.numTransactionsRunning)
            analysis_id = transaction.create_analysis_record()

            #Generate response to server before kicking off analysis
            self.write(json.JSONEncoder().encode({'analysis_id': str(analysis_id)}))
            self.finish()

            #Encapsulate running of analysis in a future
            print("Submitting analysis " + str(analysis_id) + " to analysis queue")
            self.analysis_executor.submit(transaction.run, analysis_id, self)

        #Keep this error instance as a catch-all for all web requests
        except Exception as err:

            print("===========error==================")
            print(json.JSONEncoder().encode({'error': err.error}))
            print("===========end_error==================")
            self.set_status(err.code)
            self.write(json.JSONEncoder().encode({'error': err.error}))

if __name__ == "__main__":

    try:
        application = tornado.web.Application([(r"/", MainHandler)])
        application.listen(5555)
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        pass<commit_msg>Update webserver.py<commit_after>#!/usr/bin/env python
"""
The Tornado server used to receive operation requests and deliver results to the user.
"""
import json
import os

from sys import stderr
from linguine.transaction import Transaction
from concurrent.futures import ThreadPoolExecutor
from linguine.transaction_exception import TransactionException

"""
Check to ensure Tornado is installed
"""
try:
    import tornado.ioloop
    import tornado.web
except ImportError:
    sys.stderr.write("Tornado not found.")

class MainHandler(tornado.web.RequestHandler):
    numTransactionsRunning = 0
    try:
        maxThreadPoolWorkers = int(os.environ['LINGUINE_THREADS'])
    except KeyError as err:
        maxThreadPoolWorkers = 2
    print("starting thread pool with " + str(maxThreadPoolWorkers) + " threads.")
    analysis_executor = ThreadPoolExecutor(max_workers=maxThreadPoolWorkers)

    def post(self):
        self.set_header('Content-Type', 'application/json')
        try:
            self.numTransactionsRunning+=1
            transaction = Transaction()
            requestObj = transaction.parse_json(self.request.body)
            transaction.read_corpora(transaction.corpora_ids)
            transaction.calcETA(self.numTransactionsRunning)
            analysis_id = transaction.create_analysis_record()

            #Generate response to server before kicking off analysis
            self.write(json.JSONEncoder().encode({'analysis_id': str(analysis_id)}))
            self.finish()

            #Encapsulate running of analysis in a future
            print("Submitting analysis " + str(analysis_id) + " to analysis queue")
            self.analysis_executor.submit(transaction.run, analysis_id, self)

        #Keep this error instance as a catch-all for all web requests
        except Exception as err:

            print("===========error==================")
            try:
                print(json.JSONEncoder().encode({'error': err.error}))
            except AttributeError as e:
                print(json.JSONEncoder().encode({'error': e.error}))
            print("===========end_error==================")
            self.set_status(err.code)
            self.write(json.JSONEncoder().encode({'error': err.error}))

if __name__ == "__main__":

    try:
        application = tornado.web.Application([(r"/", MainHandler)])
        application.listen(5555)
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        pass
{'fraction_non_alphanumeric': 0.11780538302277432, 'fraction_numerical': 0.002898550724637681, 'mean_word_length': 3.1114893617021275, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7518772', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1240, 'n_words': 303}