<commit_before>import asyncio

from thriftpy.transport import TTransportException

from aiothrift.processor import TProcessor
from aiothrift.protocol import TBinaryProtocol
from aiothrift.transport import TTransport


class Server:
    def __init__(self, processor, protocol_cls=TBinaryProtocol):
        self.processor = processor
        self.protocol_cls = protocol_cls

    @asyncio.coroutine
    def __call__(self, reader, writer):
        itransport = TTransport(reader)
        iproto = self.protocol_cls(itransport)
        oproto = self.protocol_cls(writer)
        while not reader.at_eof():
            try:
                yield from self.processor.process(iproto, oproto)
            except TTransportException:
                pass
            except Exception:
                pass
        writer.close()


@asyncio.coroutine
def make_server(service, handler,
                host="localhost", port=9090,
                loop=None,
                protocol_cls=TBinaryProtocol,
                ):
    processor = TProcessor(service, handler)
    if loop is None:
        loop = asyncio.get_event_loop()
    server = yield from asyncio.start_server(
        Server(processor, protocol_cls), host, port, loop=loop)
    return server<commit_msg>close streamwriter on exception<commit_after>import asyncio

from thriftpy.transport import TTransportException

from aiothrift.processor import TProcessor
from aiothrift.protocol import TBinaryProtocol
from aiothrift.transport import TTransport


class Server:
    def __init__(self, processor, protocol_cls=TBinaryProtocol):
        self.processor = processor
        self.protocol_cls = protocol_cls

    @asyncio.coroutine
    def __call__(self, reader, writer):
        itransport = TTransport(reader)
        iproto = self.protocol_cls(itransport)
        oproto = self.protocol_cls(writer)
        while not reader.at_eof():
            try:
                yield from self.processor.process(iproto, oproto)
            except TTransportException:
                writer.close()
            except Exception:
                # app exception
                writer.close()
        writer.close()


@asyncio.coroutine
def make_server(service, handler,
                host="localhost", port=9090,
                loop=None,
                protocol_cls=TBinaryProtocol,
                ):
    processor = TProcessor(service, handler)
    if loop is None:
        loop = asyncio.get_event_loop()
    server = yield from asyncio.start_server(
        Server(processor, protocol_cls), host, port, loop=loop)
    return server
{'fraction_non_alphanumeric': 0.0708204334365325, 'fraction_numerical': 0.0030959752321981426, 'mean_word_length': 2.460508701472557, 'pattern_counts': {'":': 0, '<': 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': '1026032', 'n_tokens_mistral': 695, 'n_tokens_neox': 667, 'n_words': 168}