<commit_before>import logging
from amqplib.client_0_8 import Message as AmqplibMessage

from tamqp import slave
from tamqp import message

logger = logging.getLogger('tamqp')

#proxy class for amqplib.client_0_8.Message.
#until http://code.google.com/p/py-amqplib/issues/detail?id=15 is fixed
class _Message(object):

    def __init__(self, body, children=None, **properties):
        self.body = body
        self.properties = properties

    def to_amqplib_message(self):
        return AmqplibMessage(self.body, **self.properties)

    @classmethod
    def from_amqplib_message(cls, msg):
        return cls(msg.body, **msg.properties)

class AmqpSlave(object):

    def __init__(self, channel_factory, on_start=None, io_loop=None):
        assert on_start is None or callable(on_start)
        self.on_start = on_start
        self.channel_factory = channel_factory
        self.slave = slave.SlaveProcess(self._slave_main, io_loop)
        self.slave.start()
        self.message_stream = message.MessageStream(self.slave.iostream)

    def stop(self):
        self.message_stream.close()
        self.slave.stop()

    def _slave_main(self, socket):
        if self.on_start:
            self.on_start()
        message_socket = message.MessageSocket(socket)
        ch = self.channel_factory()
        try:
            self._slave_impl(ch, message_socket)
        finally:
            conn = ch.connection
            ch.close()
            conn.close()
            logger.info("closed amqp connection")

class AmqpConsumer(AmqpSlave):

    def __init__(self, channel_factory, queue_name, callback, on_start=None,<commit_msg>close, but prevent spurious errors<commit_after>import logging
from amqplib.client_0_8 import Message as AmqplibMessage

from tamqp import slave
from tamqp import message

logger = logging.getLogger('tamqp')

#proxy class for amqplib.client_0_8.Message.
#until http://code.google.com/p/py-amqplib/issues/detail?id=15 is fixed
class _Message(object):

    def __init__(self, body, children=None, **properties):
        self.body = body
        self.properties = properties

    def to_amqplib_message(self):
        return AmqplibMessage(self.body, **self.properties)

    @classmethod
    def from_amqplib_message(cls, msg):
        return cls(msg.body, **msg.properties)

class AmqpSlave(object):

    def __init__(self, channel_factory, on_start=None, io_loop=None):
        assert on_start is None or callable(on_start)
        self.on_start = on_start
        self.channel_factory = channel_factory
        self.slave = slave.SlaveProcess(self._slave_main, io_loop)
        self.slave.start()
        self.message_stream = message.MessageStream(self.slave.iostream)

    def stop(self):
        self.message_stream.close()
        self.slave.stop()

    def _slave_main(self, socket):
        if self.on_start:
            self.on_start()
        message_socket = message.MessageSocket(socket)
        ch = self.channel_factory()
        try:
            self._slave_impl(ch, message_socket)
        finally:
            if ch:
                conn = ch.connection
                ch.close()
                if conn:
                    conn.close()
            logger.info("closed amqp connection")

class AmqpConsumer(AmqpSlave):

    def __init__(self, channel_factory, queue_name, callback, on_start=None,
{'fraction_non_alphanumeric': 0.09476831091180866, 'fraction_numerical': 0.003587443946188341, 'mean_word_length': 2.8327605956471937, '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': '5065987', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1011, 'n_words': 217}